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) {
if(!test.isRunningTest()){
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];
system.debug('@@@@'+jcode);
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;
try{
upsert o;
}catch(DMLException ex){
ApexPages.addMessages(ex);
}
return null;
}
}
Visualforce Page:
<apex:page standardController="OrderItem" showHeader="false" sidebar="false" extensions="OrderItemExtension">
<apex:pageBlockSectionItem>
<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:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem>
<apex:outputLabel value="Defect Codes" for="defectcodes"/>
<apex:selectList value="{!defectcode}" size="1" id="defectcodes" disabled="{!ISNULL(sourcecode)}">
<apex:selectOptions value="{!defectcodes}"></apex:selectOptions>
</apex:selectList>
</apex:pageBlockSectionItem>
</apex:page>