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
iswarya sekar 7iswarya sekar 7 

how to display fields based on picklist values

Hi everyone,

I have a picklist field called Engagement_model__c in Opportunity. it has the values
1.Fixed Bid
2.Time&Material
3.Fix Capacity

If 1 is selected it should display some 5 custom fields and values should be entered, and record should be saved.
SKSANJAYSKSANJAY
Hi Iswarya,

If you are looking for standard Opportunity New Record page functionality, then it is not possible.

This can be done by creating a visualforce page and controller to implement Opportunity record creation and there you can use ReRender and rendered property to hide fields as per your requirement.

Hope this will help you.
Thanks,
Sanjay
iswarya sekar 7iswarya sekar 7
<apex:page standardController="Opportunity">
    <apex:form>
        
        <apex:pageBlock title="Enter Opportunity Information">
            
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!Save}" immediate="true"/>
                <apex:commandButton value="Cancel" action="{!Cancel}" immediate="true"/>                               
            </apex:pageBlockButtons>
            
            <apex:PageblockSection columns="1" >
                <apex:inputField value="{!Opportunity.Name}"/>
                <apex:PageBlockSectionItem >
                    <apex:outputLabel value="Engagement_Model__c"/>
                    <apex:actionRegion >
                        <apex:inputField value="{!Opportunity.Engagement_Model__c}">
                            <apex:actionSupport event="onchange" reRender="ajaxrequest" />
                        </apex:inputField>
                    </apex:actionRegion>
                </apex:PageBlockSectionItem>
            </apex:PageblockSection>
            
            <apex:outputPanel id="ajaxrequest">  
                <apex:pageBlockSection rendered="{!Opportunity.Engagement_Model__c=='Fixed Bid'}" >
                    
                    <apex:inputField value="{!Opportunity.Start_date__c}"/>
                    <apex:inputField value="{!Opportunity.Duration__c}"/>
                    <apex:inputField value="{!Opportunity.Probability__c}"/>
                    <apex:inputField value="{!Opportunity.Value__c}"/>
                    
                </apex:pageBlockSection>
                
                <apex:pageBlockSection rendered="{!Opportunity.Engagement_Model__c=='Fix capacity'}" >
                    
                    <apex:inputField value="{!Opportunity.Start_date__c}"/>
                    <apex:inputField value="{!Opportunity.Duration__c}"/>
                    <apex:inputField value="{!Opportunity.Probability__c}"/>
                    <apex:inputField value="{!Opportunity.rate_hr__c}"/>
                    <apex:inputField value="{!Opportunity.No_of_Resource__c}"/>
                    
                </apex:pageBlockSection>
                
                <apex:pageBlockSection rendered="{!Opportunity.Engagement_Model__c=='Time&Material'}" >
                    
                    <apex:inputField value="{!Opportunity.Start_date__c}"/>
                    <apex:inputField value="{!Opportunity.Duration__c}"/>
                    <apex:inputField value="{!Opportunity.Probability__c}"/>
                    <apex:inputField value="{!Opportunity.rate_hr__c}"/>
                    <apex:inputField value="{!Opportunity.Hours_Month__c}"/>
                    
                </apex:pageBlockSection>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

THIS IS MY CODE. but when i click save, the record is not saved.
SKSANJAYSKSANJAY
Could you help me showing your controller code as well?
iswarya sekar 7iswarya sekar 7
public class page3displays{
    Public Opportunity opp{get;set;}
    public ID oppid{get;set;} // used to pass values from controller to VF page and ViceVersa.
    
    public page3displays(ApexPages.StandardController controller) {    // Default Constructor Method to fetch data.
        oppid = apexpages.currentpage().getparameters().get('Id');
        
        if(oppid!=null){
            opp =[select Id, Name, Probability__c, Duration__c, Hours_Month__c, No_of_Resource__c, rate_hr__c,Start_date__c FROM Opportunity where id=:oppid]; // using ID, it displays the CaseNumber to indicate which Case need to be Closed.
            system.debug('the value of case is'+opp); // To Find and Resolve problems in execution.
        } 
    }
    
    public Pagereference saveMethod(){
        try{                                
            update opp;
            Pagereference tocase = new Pagereference('/'+oppid); // Redirects to New VisualForce Page.
            tocase.setRedirect(true);
            return tocase;
        }
        catch(Exception e){
            Apexpages.addMessages(e);   // It displays Error Message when Validation rule fires.
        }
        return null;  
    }
}

My controller class
SKSANJAYSKSANJAY
Few things you need to understand, while using standardsetcontroller save method, you must present the required fields onto the page so that user can enter those data. You are missing Name, StageName, Closed Date etc fields on the page eventhough you are trying to bypass validation rule by usiing Immediate attribute on save button.

A very simple way to find the error onto the page is to just add pageMessages tag just after apex:page tag. FYI
<apex:page standardController="Opportunity">
    <apex:pageMessages />
    <apex:form >
        
        <apex:pageBlock title="Enter Opportunity Information">
...
Hope this will help you

Thanks,
Sanjay
iswarya sekar 7iswarya sekar 7
Hi Sanjay,

Save button is working. but my issue is when I'm giving values in custom fields, after clicking save those values are not updating in that particular opportunity.
SKSANJAYSKSANJAY
Try removing Immediate = "true" from save button.
iswarya sekar 7iswarya sekar 7
Yes i removed. but its not working
SKSANJAYSKSANJAY
For me its working while removing immediate="true" from save button. It is updating the record which I have supplied Opp Id at the URL. Did you add pageMessages to the page? It will help you identify the issue.
iswarya sekar 7iswarya sekar 7
s i added. i dont know what the pblm is.