1

I am trying to figure out how to render a Visualforce component containing inputfields conditionally, depending on a stage field outside of the visualforce page. I would like to render a different visualforce component depending on the stage. Here's what I have so far. When I run as-is, I can't get anything to display within my visualforce page.

VF Page:

<apex:form >
   <c:StageFields record="{!opportunity}"  rendered="{!opportunity.StageName == 'Received'}" /> 
</apex:form>

Component:

 <apex:component>

    <apex:attribute name="record" type="Opportunity" description="Opportunity"/>
    <apex:inputField value="{!record.Rep_Name__c}"/>

</apex:component>

2 Answers 2

2

You'll want to pass the correct opportunity to the component so that your page's save method will work correctly. Usually, I pass the standard controller and/or record directly to the component. This requires some cooperation at the page level, like this:

public class StandardControllerExtension {
    public SObject record { get; set; }
    public ApexPages.StandardController standardController { get; set; }

    public StandardControllerExtension(ApexPages.StandardController controller) {
        record = controller.getRecord();
        standardController = controller;
    }
}

Which you can then add to your page:

<apex:page standardController="Opportunity" extensions="StandardControllerExtension">
    <apex:form >
         <c:StageFields record="{!record}" rendered="{!opportunity.StageName = 'Received'}" />
    </apex:form>
</apex:page>

And to your component:

<apex:component>
    <apex:attribute name="record" type="Opportunity" description="Opportunity"/>
    <apex:inputField value="{!record.Name}"/>
</apex:component>

Edit: The attribute wasn't formatted as a proper merge field, so it would cause the component to never render. I updated the code in this answer to correct that problem.

7
  • Thanks! I'm still unable to render the fields. My new code is update above. Using your public class as well. Commented Apr 11, 2016 at 16:38
  • @ErikEitel I'm working on a mock-up. Give me a few minutes. Commented Apr 11, 2016 at 16:56
  • @ErikEitel Your rendered attribute is incorrect. Merge fields always start with {!, but you started it with {(!, so it doesn't get evaluated, and therefore doesn't equal true, which therefore causes the element to not render. Change the attribute to {!Opportunity.StageName='Received'}. Commented Apr 11, 2016 at 17:00
  • Awesome. Worked like a charm! Thanks a ton! Updated code above to reflect changes. Commented Apr 11, 2016 at 17:03
  • Related... Any idea why this code wouldn't process an inline edit? Switched inputfields to all output fields and added apex:inlineeditsupport, but still no go. I click save and it stores the value, but then is gone on page refresh. Commented Apr 11, 2016 at 20:18
0

First you need to make your component parameter ready by passing Opportunity id so that you can query that and display opportunity details inside component or with your current implementation pass all required value from your visualforce page which you are displaying using your component in following way

<c:Stagefields opportunity="oppname"/>
3
  • Thanks. I just updated the code above to include your edit. Still getting the same result. Did I do that right/what am I missing? Commented Apr 11, 2016 at 16:26
  • Pass value in rep_name__c field as opportunity.rep_name__c Commented Apr 11, 2016 at 16:30
  • I am on mobile so can't post exact solution. You can go for sfdcfox solution i am suggesting the same. Commented Apr 11, 2016 at 16:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.