Trigger for Update Related Contacts Picklist based on Account Picklist in Salesforce

By | February 12, 2021

Apex Trigger

trigger UpdateRelatedContactsBasedOnAccount on Account (before update, after update) {
    Set<id> accid = new Set<id>();
    for(Account a : Trigger.new){
        if(a.Rating != null){
            accid.add(a.id);
        }
        if(!accid.isEmpty()){
            List<Contact> conList = [select Id, Name, AccountId, Level__c from Contact where AccountId =: accid];
            if(!conList.isEmpty()){
                for(Contact c : conList){
                    if(a.Rating == 'Hot'){
                    c.Level__c='Primary';
                    }else if(a.Rating == 'Warm'){
                    c.Level__c='Secondary';
                    }else if(a.Rating == 'Cold'){
                    c.Level__c='Tertiary';
                    }
                }
                update conList;
            }
        }
    }
}
Share this...
Share on Facebook
Facebook
Email this to someone
email

Leave a Reply

Your email address will not be published. Required fields are marked *