function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sales4cesales4ce 

Render based on the picklist Value selection

Hi,

 

I am unable to get this to work.

I am trying to render a pageblock based on the selection of the picklist value. if the Opportunitystage=='Qualification' i want to then render the close date field.

in the below code i want to display the Closedate only when an Opportunity stage is selected and equals to "Qualification"

I don't know if i am doing any AJAX requests incorrectly.Can any one help me with this.

 

Page:

<apex:page standardController="Opportunity">
  <apex:form >
    <apex:pageBlock title="Edit Opportunity" id="thePageBlock" mode="edit">
   
      <apex:pageBlockButtons location="bottom" >
        <apex:commandButton value="Save" action="{!save}"/>
        <apex:commandButton value="Cancel" action="{!cancel}"/>               
      </apex:pageBlockButtons>

    <apex:pageBlockSection columns="1">
      <apex:inputField value="{!opportunity.name}"/>
      <apex:pageBlockSectionItem >
      <apex:outputLabel value="{!$ObjectType.opportunity.fields.stageName.label}" 
                        for="stage"/>
      <!-- 
           Without the actionregion, selecting a stage from the picklist would cause 
           a validation error if you hadn't already entered data in the required name 
           and close date fields.  It would also update the timestamp.
      -->  
    
      <apex:actionRegion renderRegiononly="{!opportunity.stageName !='Qualification'}" >
        <apex:inputField value="{!opportunity.stageName}" id="stage">
          <apex:actionSupport event="onchange" rerender="ajaxrequest"
                              status="status"/>
          </apex:inputField>
          </apex:actionRegion>
      </apex:pageBlockSectionItem>
        
       
        </apex:pageBlockSection>
        
          <apex:outputPanel id="ajaxrequest" rendered="{!Opportunity.StageName}" > 
              <apex:inputField value="{!Opportunity.CloseDate}"/>
             {!text(now())}
          </apex:outputPanel> 
        
      </apex:pageBlock>
    </apex:form>
</apex:page>

Thanks,

Sales4ce

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

First of all you need to remove the renderRegionOnly attribute on your actionRegion component -- that is not doing what you think it's doing.  Simply surrounding your inputField with an actionRegion will submit only that inputField without submitting the values outside of it (as you've alluded to in your HTML comment).

 

Whatever you want to be conditionally rendered should have rendered="{!Opportunity.StageName != 'Qualification'}" because that is the expression that will be evaluated whenever that component is created or updated.

 

And then finally, the target of what you are rerendering needs to always be on your page -- in more basic terms, whatever id you specify in rerender="someId" cannot be on the same component that has the rendered="{!Opportunity.StageName != 'Qualification'}" on it.  So if you want your outputPanel conditionally rendered, it can't be the target of your rerender attribute.  You may need to put it inside of another outputPanel.  For more explanation see this thread: http://boards.developerforce.com/t5/Visualforce-Development/Dynamic-hiding/m-p/79589

All Answers

jwetzlerjwetzler

First of all you need to remove the renderRegionOnly attribute on your actionRegion component -- that is not doing what you think it's doing.  Simply surrounding your inputField with an actionRegion will submit only that inputField without submitting the values outside of it (as you've alluded to in your HTML comment).

 

Whatever you want to be conditionally rendered should have rendered="{!Opportunity.StageName != 'Qualification'}" because that is the expression that will be evaluated whenever that component is created or updated.

 

And then finally, the target of what you are rerendering needs to always be on your page -- in more basic terms, whatever id you specify in rerender="someId" cannot be on the same component that has the rendered="{!Opportunity.StageName != 'Qualification'}" on it.  So if you want your outputPanel conditionally rendered, it can't be the target of your rerender attribute.  You may need to put it inside of another outputPanel.  For more explanation see this thread: http://boards.developerforce.com/t5/Visualforce-Development/Dynamic-hiding/m-p/79589

This was selected as the best answer
sales4cesales4ce

Thanks for your timely reply. i have modified the code per your advice and it worked like charm.

Thanks for your help.

 

This is the updated/working code for future reference.

 

<apex:page standardController="Opportunity">
      <apex:form >
          <apex:pageBlock title="Enter Opportunity Information">
              <apex:pageBlockButtons location="bottom">
                  <apex:commandButton value="Save" action="{!Save}"/>
                  <apex:commandButton value="Cancel" action="{!Cancel}" immediate="true"/>                                
              </apex:pageBlockButtons>
              <apex:PageblockSection columns="1" >
                      <apex:inputField value="{!Opportunity.Name}"/>
                     <apex:PageBlockSectionItem >
                         <apex:outputLabel value="Stage"/>
                         <apex:actionRegion >
                          <apex:inputField value="{!Opportunity.StageName}">
                          <apex:actionSupport event="onchange" reRender="ajaxrequest" />
                          </apex:inputField>
                         </apex:actionRegion>
                      </apex:PageBlockSectionItem>
                  </apex:PageblockSection>
              <apex:outputPanel id="ajaxrequest">   
                  <apex:pageBlockSection rendered="{!Opportunity.StageName=='Prospecting'}" >
                      
                      <apex:inputField value="{!Opportunity.CloseDate}"/>
              
                  </apex:pageBlockSection>
              </apex:outputPanel> 
          </apex:pageBlock>
      </apex:form>
SanchSanch

Thank you for posting the working code. This helped resolve my issue.

ami anuami anu

Thank You for the solution this helped me a lot

Lago S.p.a.Lago S.p.a.
Hi , thanks for your code, I have a similar problem, but I need for each value of a picklist to display  differents section ,depending from the picklist value.
Is it possible with VForce?