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
Curious ForceCurious Force 

Render fields based on picklist value

Any help would be appreciated. I'm trying to get a field to render based on a picklist value seleciton but I can't seem to get it to work. Here's my VF code (I'm not using a controller).

 

<apex:page standardController="Opportunity">
 <apex:form >
<apex:pageBlock id="thePageBlock">
<apex:pageBlockSection columns="2">
<apex:inputfield value="{!Opportunity.Type}" id="Type">
<apex:actionSupport event="onchange" 
rerender="ProdFamilyModel"/> 
</apex:inputfield>
<apex:inputfield value="{!Opportunity.Fiscal_Booking_Date__c}"/> 
</apex:pageBlockSection>
<apex:outputPanel id="ProdFamilyModel" rendered="{!IF(Opportunity.Type != 'Test', true, false)}">
<apex:pageBlockSection columns="2" id="ProdFamily"> 
<apex:inputfield value="{!Opportunity.Product_Family__c}"/>
</apex:pageBlockSection> 
</apex:outputPanel> 
</apex:pageBlock>
</apex:form> 
</apex:page>

 

What am I doing wrong?

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Sometimes, if you have any of the required fields in your visuaforce page, then none of the actions(It may be actionsupport,commondbutton, commandLink, etc) are invoked.

So, always have a pages message and rerender that to find whether you are hitting required field issue.

 

I have used actionRegion tag in your code, check if this helps you. If not then let me know what didn not worked.

<apex:page standardController="Opportunity">
	<apex:form >
		<apex:pageMessages id="theMessage"/>
		<apex:pageBlock id="thePageBlock">
		<apex:pageBlockSection columns="2">
			<apex:actionRegion>
				<apex:inputfield value="{!Opportunity.Type}" id="Type">
					<apex:actionSupport event="onchange" rerender="ProdFamilyModel, theMessage"/> 
				</apex:inputfield>
			</apex:actionRegion>
			<apex:inputfield value="{!Opportunity.Fiscal_Booking_Date__c}"/> 
		</apex:pageBlockSection>
		<apex:outputPanel id="ProdFamilyModel">
			<apex:pageBlockSection columns="2" id="ProdFamily" rendered="{!IF(Opportunity.Type != 'Test', true, false)}"> 
				<apex:inputfield value="{!Opportunity.Product_Family__c}"/>
			</apex:pageBlockSection> 
		</apex:outputPanel> 
		</apex:pageBlock>
	</apex:form> 
</apex:page>

 Note: Use Insert code button from rich text panel for inserting your code next time along with proper intendation(It helpful for us to read your code :) ).

All Answers

Rahul SharmaRahul Sharma

Sometimes, if you have any of the required fields in your visuaforce page, then none of the actions(It may be actionsupport,commondbutton, commandLink, etc) are invoked.

So, always have a pages message and rerender that to find whether you are hitting required field issue.

 

I have used actionRegion tag in your code, check if this helps you. If not then let me know what didn not worked.

<apex:page standardController="Opportunity">
	<apex:form >
		<apex:pageMessages id="theMessage"/>
		<apex:pageBlock id="thePageBlock">
		<apex:pageBlockSection columns="2">
			<apex:actionRegion>
				<apex:inputfield value="{!Opportunity.Type}" id="Type">
					<apex:actionSupport event="onchange" rerender="ProdFamilyModel, theMessage"/> 
				</apex:inputfield>
			</apex:actionRegion>
			<apex:inputfield value="{!Opportunity.Fiscal_Booking_Date__c}"/> 
		</apex:pageBlockSection>
		<apex:outputPanel id="ProdFamilyModel">
			<apex:pageBlockSection columns="2" id="ProdFamily" rendered="{!IF(Opportunity.Type != 'Test', true, false)}"> 
				<apex:inputfield value="{!Opportunity.Product_Family__c}"/>
			</apex:pageBlockSection> 
		</apex:outputPanel> 
		</apex:pageBlock>
	</apex:form> 
</apex:page>

 Note: Use Insert code button from rich text panel for inserting your code next time along with proper intendation(It helpful for us to read your code :) ).

This was selected as the best answer
Curious ForceCurious Force

Hey, Rahul. Thanks for your help. I used you code and it didn't work. As well, I didn't get a failure mesage either.

Rahul SharmaRahul Sharma
Id and rendered don't really like each other. :P
In this case, the component is not already loaded in DOM and you are trying to rerender that component(which is not in dom).
To overcome this make outer panel Rendered and rerender inner page block section.
I have updated the code in my earlier post. Check it out.
Curious ForceCurious Force

Hey, Rahul. Sorry, still no luck. :(

Rahul SharmaRahul Sharma
I tried the code and it is working for me. On change of Type, the Product family section is rendered conditionally.
Please check if you have value as 'Test' in the Opportunity Type picklist.
Curious ForceCurious Force

It wasn't working for me yesterday but it works this morning. 

 

Thanks a bunch for your help, Rahul. You are a life saver!!!

GulatiGulati

Hi there,

 

I am having same issue. I have tried to do the same thing but it is not working, you can review my code and pls suggest me a solution soon. 

<apex:page standardController="Lead">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:actionRegion >
    <apex:inputField value="{!Lead.Status}" id="Status">
    <apex:actionSupport event="onChange" reRender="section"/>
    </apex:inputField>
</apex:actionRegion>
<apex:inputField value="{!Lead.Phone}"/>
</apex:pageBlockSection>
<apex:outputPanel id="section">
<apex:pageBlockSection columns="2" id="ProdFamily" rendered="{!IF(Lead.Status=='Working - Contacted',true,false)}">
<apex:inputField value="{!Lead.website}"/>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>