On OrderItem, I have 2 picklist fields MTO_Source_Code__c and Defect_Code__c where I'm trying to query 2 object's records and display as picklist values. The 2nd picklist values should render based on 1st selected picklist value, this rendering is not working on initial edit page selection but renders fine after saving the record and opening again

Apex Controller:

public class OrderItemExtension {

    public final OrderItem o;     
    public String sourcecode {get;set;}
    public String defectcode {get;set;}
    public String getSourcecode() { return this.sourcecode; }
    public void setSourcecode(String s) { this.sourcecode = s; }
    public String getDefectcode() { return this.defectcode; } 
    public void setDefectcode(String s) { this.defectcode = s; }     

    public RMS_OrderItemExtension(ApexPages.StandardController stdController) {
            stdController.addFields(new List<String>{'OrderId','Service__c', 'Installed_Product_Name__c', 'Product_Name__c','MTO_Source_Code__c','Defect_Code__c'});           

        this.o = (OrderItem)stdController.getRecord();      

   public List<SelectOption> getsourcecodes() {
      if (o.MTO_Source_Code__c != NULL){
        sourcecode = o.MTO_Source_Code__c;
      List<SelectOption> optionList = new List<SelectOption>();

      optionList.add(new SelectOption('','- Select source code -'));

       for(Service_Product_Source_Codes__c sc : [select Product_Name__c, Code_Name__c, Name from Service_Product_Source_Codes__c where Service_Product__c IN (Select product2Id from PricebookEntry where Id=: o.pricebookentryid)]){
        optionList.add(new SelectOption(sc.Code_Name__c,sc.Code_Name__c));
      return optionList; 

    public List<SelectOption> getdefectcodes() {
      if(o.Defect_Code__c != NULL){
      defectcode = o.Defect_Code__c;
      List<SelectOption> optionList = new List<SelectOption>();
      optionList.add(new SelectOption('', '- Select Defect Code -')); 

      system.debug('@@@@'+ sourcecode);
      if(sourcecode != NULL) {
         List<Service_Product_Source_Codes__c> jcode = [select MTO_Source_Codes__c from Service_Product_Source_Codes__c where Code_Name__c =: sourcecode];
         for (MTOSourceCode_DefectCode__c df : [select Name, Defect_Code_Name__c, MTO_Source_Code__r.Name from MTOSourceCode_DefectCode__c df where df.MTO_Source_Code__c =: jcode[0].MTO_Source_Codes__c]){
          optionList.add(new SelectOption(df.Defect_Code_Name__c,df.Defect_Code_Name__c));
      return optionList;

    public PageReference saveCloseTab(){
           o.MTO_Source_Code__c = sourcecode;
           o.Defect_Code__c = defectcode; 

            upsert o;           
        }catch(DMLException ex){
        return null;


Visualforce Page:

<apex:page standardController="OrderItem" showHeader="false" sidebar="false" extensions="OrderItemExtension">

       <apex:outputLabel value="MTO Source Code" for="sourcecode"/>
           <apex:selectList value="{!sourcecode}" size="1" id="sourcecode"> 
             <apex:actionSupport event="onchange"  rerender="defectcodes"/> 
                <apex:selectOptions value="{!sourcecodes}"></apex:selectOptions>

         <apex:outputLabel value="Defect Codes" for="defectcodes"/>
             <apex:selectList value="{!defectcode}" size="1" id="defectcodes" disabled="{!ISNULL(sourcecode)}"> 
               <apex:selectOptions value="{!defectcodes}"></apex:selectOptions>

  • Please do not remove your code after receiving an answer. This behavior is considered vandalism. – Adrian Larson Jul 6 '18 at 20:50

You have created the two picklist fields without any dependency between values. Only thing you have done is rendering 2nd picklist in actionSupport but it will not render values based on the selection of first picklist.

That's why, initially, when values are not saved, 2nd picklist values are not displaying and after saving it is displaying 2nd picklist value which is not even dependent on first picklist. Values are getting retrieved and displayed.

To create actual dependency between them, is to create a Map at controller which could hold first Picklist as Key and 2nd picklist as values and then during selection pass the values to controller and refresh the 2nd picklist.

  • Thanks @Barol, but the actual issue is bcoz the field required on other fields on edit page we need to use the actionregion for rendering the actionsupport. – Salesforce Learner Jul 6 '18 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.