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
Dragon SlayerDragon Slayer 

Help required with a Trigger

Hi,

I am trying to create a trigger on the Opportunity object so that whenever a particular field (e.g. ProdType) gets updated or is inserted, the value of the field should be copied to a field within a custom object (lets say CusObj.ProdType). The CusObj has a lookup relation with Opportunity.
Can somebdoy help me with this please?
I tried creating a simple formula earlier but that doesn't seem to help because the field ProdType is a multi-select picklist so there can be more than one selections at a time. I could have written an IF condition had it been just a simple pick-list because there could have been only one selection at a time. But, since there can be lot of permutations and combinations, I don't think that would work so the only option left for me is to create a trigger.
Ross GilbertRoss Gilbert
try this trigger on opportunity:

trigger updateChildCustomObject on Opportunity (after update){

    List<CustObj__c> objectsToUpdate = new List<CustObj__c>();
    
    for(CustObj__c co :[select Id, Opportunity__c,Opportunity__r.ProdType__c,ProdType__c from CustObj__c where 
       Opportunity__c in : trigger.new]){
        co.ProdType__c = co.Opportunity__r.ProdType__c;
        objectsToUpdate.add(co);
    }
    
    if(!objectsToUpdate.isEmpty()){
        update objectsToUpdate;
    }
}

basically what that does is, when an opp is updated, if it's got value(s) in the ProdType multiselect picklist field, those value(s) will copy over to any records on the associated custom object records
Ross GilbertRoss Gilbert
Forgot to add that...the field in that code called "ProdType__c" is a multiselect picklist type field on both opportunity and on CustObject__c custom object