Wednesday, January 18, 2023

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

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;
            }
        }
    }
}

No comments:

Post a Comment

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...