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
Deepak Singh 116Deepak Singh 116 

Need a trigger for update contact picklist field when account status updated

I have two status picklist field one at contact and one at account.Both the picklist have the same value (Active,Inactive,closed).Now i want when i update the status on account then the contactstatus is automatically update as account. 
Best Answer chosen by Deepak Singh 116
Ajay K DubediAjay K Dubedi
Hi Deepak,

Please try this below code. This code is an easy and simple way to understand for new Salesforce Developer.

//Apex Helper Class//

public class AccountUpdateContactHelperClass {
    
    public static void updateContact(List<Account> accList){
        Set<Id> setOfAccIds = new Set<Id>();
        System.debug('Account status'+accList);
        
        for(Account ac:accList){
            if(ac.Id!=null){
                setOfAccIds.add(ac.Id);
            }
        }
        List<Contact> conList = new List<Contact>();
        conList = [SELECT ID ,LastName,Status__c FROM Contact WHERE AccountId IN: setOfAccIds];
        List<Contact> conList1 = new List<Contact>();
        for(Account a:accList){
            System.debug('Account status'+a.Status__c);
            for(Contact con:conList){
                System.debug('Cont status'+con.Status__c);
                con.AccountId =a.Id;
                con.Status__c = a.Status__c;
                conList1.add(con);
            }
        }
        if(conList1.size()>0){
            update conList1;
        }
    }
}

//Trigger Class//

trigger AccountTrigger on Account (after update) {
AccountUpdateContactHelperClass.updateContact(Trigger.New);
}
 
Please mark it as best if you find it helpful.
Thank You
Ajay Dubedi

 

All Answers

Abdul KhatriAbdul Khatri
Please find the code. I am not sure what is the API Name of the Status field, I just made it up for the sake of code. Please change the API Name of the Stauts field as per you need.
 
trigger UpdateContactStatus on Account (after update) {
    
    List<Id> idList = new List<Id>();
    
    for(Account acct : trigger.new) {
        
        if(acct.Status__c == oldMap.get(acct.Id).Status__c) continue;
        
        idList.add(acct.Id);
        
    }

    
    List<Account> accountList = [SELECT Status__c, (SELECT Status__c FROM Contacts) FROM Account WHERE Id = :idList];
    
    List<Contact> contactToUpdatList = new List<Contact>();
    
    for(Account acct : accountList) {
        
        if(acct.Contacts == null) continue;
        
        for(Contact contact : acct.Contacts) {
            
            contact.Status__c = acct.Status__c;
            contactToUpdatList.add(contact);
        }
    }
    
    if(!contactToUpdatList.isEmpty())
        update contactToUpdatList;
}

 
Ashish MalikarAshish Malikar
Hi Deepak, please find below code. API name is considered as status.
trigger ContactStatusUpdate on Account (after update) {
    
    // Get all contact record using map in Trigger variable.
    List<Contact> contactList = [SELECT Id, accountId, status__c FROM Contact where accountId IN : Trigger.newMap.keySet()];
    
    // Update status of all related Contact     
    for(Contact c:contactList){
            c.status__c = Trigger.newMap.get(c.accountId).status__c;
        }
    // update contact List
    update contactList;
}
Ajay K DubediAjay K Dubedi
Hi Deepak,

Please try this below code. This code is an easy and simple way to understand for new Salesforce Developer.

//Apex Helper Class//

public class AccountUpdateContactHelperClass {
    
    public static void updateContact(List<Account> accList){
        Set<Id> setOfAccIds = new Set<Id>();
        System.debug('Account status'+accList);
        
        for(Account ac:accList){
            if(ac.Id!=null){
                setOfAccIds.add(ac.Id);
            }
        }
        List<Contact> conList = new List<Contact>();
        conList = [SELECT ID ,LastName,Status__c FROM Contact WHERE AccountId IN: setOfAccIds];
        List<Contact> conList1 = new List<Contact>();
        for(Account a:accList){
            System.debug('Account status'+a.Status__c);
            for(Contact con:conList){
                System.debug('Cont status'+con.Status__c);
                con.AccountId =a.Id;
                con.Status__c = a.Status__c;
                conList1.add(con);
            }
        }
        if(conList1.size()>0){
            update conList1;
        }
    }
}

//Trigger Class//

trigger AccountTrigger on Account (after update) {
AccountUpdateContactHelperClass.updateContact(Trigger.New);
}
 
Please mark it as best if you find it helpful.
Thank You
Ajay Dubedi

 
This was selected as the best answer
Deepak Singh 116Deepak Singh 116

Thanks Ajay it works.
Abdul KhatriAbdul Khatri
Deepak, Just for you information. Ajay's code contains many best practise code violations and you may end up crossing the governor's LIMIT so be watchful for that.
Deepak Singh 116Deepak Singh 116
Abdul,
I need a logic less trigger can you guide me how to achiev using you above trigger.