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
sfuser12sfuser12 

Display field value based on selected picklist in visualforce

Hello,

I want to show Name field based on selected rating. Like if I select rating = hot then it should show name of that rating which is hot.

Rating will be input field. Name will be output field

<apex:page id="dependentvalueVF" controller="dependentvalueController" docType="html-5.0"> <apex:form>
<apex:actionFunction action="{!yourControllerMethod}" name="callTemplate"/>
<apex:pageblock>
<apex:pageBlockSection>
<apex:inputfield label="Rating" value="{!locationList.Rating}" onChange="callTemplate" />
<apex:outputField label="Name" value="{!locationList.Name}"/> </apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>

public with sharing class dependentvalueController {
public Account locationList{get;set;}
public string Rating{get;set;}
public string Name {get;set;}
public PageReference yourControllerMethod(){
locationList = [Select Name From Account Where Rating =:Rating LIMIT 1];
return null;
}
}
 
Ajay K DubediAjay K Dubedi
Hi,

You can 'Display field value based on selected picklist in visualforce' by using below code. I have tested it in my org. It is working fine.
You have done few mistakes like you have not use action function properly and not rendering the page properly.
Please update your code by below code.
  
Vf Page.

<apex:page id="dependentvalueVF" controller="dependentvalueController" docType="html-5.0"> <apex:form>
<apex:pageblock>
<apex:pageBlockSection id = "values">
    
<apex:inputField label="Rating" value="{!locationList.Rating}">
    <apex:actionSupport event="onchange" reRender="values" action="{!yourControllerMethod}" />
</apex:inputField>
        
<apex:outputField label="Name" value="{!locationList.Name}"/> </apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>


Controller

public with sharing class dependentvalueController {
public Account locationList{get;set;}
public string Rating{get;set;}
public string Name {get;set;}
public PageReference yourControllerMethod(){
locationList = [Select Name,Rating From Account Where Rating =:Rating LIMIT 1];
return null;
}
}
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi