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
Scott Bub 7Scott Bub 7 

Update picklist field from picklist field on value change

Hello Everyone,

I have found bits of code here and there and manipulated to be what I needed. It seems to work for all of the scenarios that I have tried, but I wanted to know if the way that I have written it is inefficient or not as efficient as it could be. Would anyone care to take a look at this code and tell me if they approve or if I should have done something different?

We have two picklist fields with many values on the object. We need the first field to filter down to just certain values in a visualforce page in a managed package, but you can't do that without a dependency. So, we created a second field with all of the same values to allow us to display the second one on the visualforce page with the filtered list based on the one that is selected in the original field. We didn't want to create multiple WFRs for each value so we needed to create a trigger. The trigger should only update the original field when a change has been to the secondary field.

Here is the code for the trigger:

trigger updateOrderStatusWorkOrder on SVMXC_Service_Order__c (before insert, before update) {  
    Map<String, String> myPickListMap = new Map<String, String> {
        'Open'=>'Open', 'In Progress'=>'In Progress', 'Completed'=>'Completed', 'Closed'=>'Closed', 'Cancelled'=>'Cancelled'
    };
    
    for (SVMXC_Service_Order__c wo : Trigger.new)  {
      if(trigger.isUpdate) {
        if(wo.Order_Status__c != trigger.oldMap.get(wo.Id).Order_Status__c &&
            wo.SVMXC_Order_Status__c != wo.Order_Status__c &&
            wo.Order_Status__c != NULL) {
                wo.SVMXC_Order_Status__c = myPickListMap.get(wo.Order_Status__c);        
        }
      }
      if(!trigger.isUpdate) {
          if(wo.SVMXC_Order_Status__c != wo.Order_Status__c &&
              wo.Order_Status__c != NULL){
                wo.SVMXC_Order_Status__c = myPickListMap.get(wo.Order_Status__c);  
          }
      }
    }     
}
SivaGSivaG
Hi Scott,

Small suggestion - change if(!trigger.isUpdate) -> if(trigger.isInsert)

Thanks
Kumar