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
SFDC GuestSFDC Guest 

Apex trigger to update Contact and its Cases When Account is Updated.

Hi All,

I need a trigger to update Contact Description and Contact related Case object Description whenever Account Description is updated.
Below trigger working fine for updating Contact Description when Account Description is updated. But I need to update Case records of Contact as well.

Here Account is ---> Parent,
Contact is ---> Child,
Case is ---> grand child

trigger ContactDescriptionUpdate on Account (after update) {
    Map < Id,  Account > mapAccount = new Map < Id, Account >();
    List<Contact> listContact = new List<Contact>();
    
    for(Account acct : trigger.new)
        mapAccount.put(acct.Id, acct);
    
    listContact = [ SELECT Description, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
    
    if ( listContact.size() > 0 ) {
        for ( Contact con : listContact ) {
            con.Description = mapAccount.get(con.AccountId).Description;
        }
        update listContact;
    }
}

Thanks in advance.
Best Answer chosen by SFDC Guest
Ajay K DubediAjay K Dubedi
Hi SFDC,
Please try this. Hope this will work for you.

trigger AccTrig on Account (after update) {
    UpdateCls.updateContactLeadDescription(trigger.new);
}

public class UpdateCls {
    public static void updateContactLeadDescription(List<Account> accList)
    {
        Map <Id,  Account> mapAccount = new Map <Id, Account>();     
        List<Contact> conList = new List<Contact>();
        List<Case> caseList = new List<Case>();
        
        for(Account acct : accList)
            mapAccount.put(acct.Id, acct);
        
        conList = [ SELECT id,Description, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
        
        if ( conList.size() > 0 ) {
            for ( Contact con : conList ) {
                con.Description = mapAccount.get(con.AccountId).Description;
            }
            update conList;
        } 
        
        caseList = [SELECT id,Description,ContactId,AccountId FROM Case WHERE ContactId IN:conList];
               
         if ( caseList.size() > 0 ) {
            for (Case caseObj : caseList ) {
                caseObj.Description = mapAccount.get(caseObj.AccountId).Description;
            }
            update caseList;
        }    
    }
}

Thank You
Ajay Dubedi

All Answers

SFDC GuestSFDC Guest
Hi Steven,
Getting error as : 
Compile Error: Method does not exist or incorrect signature: void keySet() from the type List<Contact> at line 17 column 102
Ajay K DubediAjay K Dubedi
Hi SFDC,
Please try this. Hope this will work for you.

trigger AccTrig on Account (after update) {
    UpdateCls.updateContactLeadDescription(trigger.new);
}

public class UpdateCls {
    public static void updateContactLeadDescription(List<Account> accList)
    {
        Map <Id,  Account> mapAccount = new Map <Id, Account>();     
        List<Contact> conList = new List<Contact>();
        List<Case> caseList = new List<Case>();
        
        for(Account acct : accList)
            mapAccount.put(acct.Id, acct);
        
        conList = [ SELECT id,Description, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
        
        if ( conList.size() > 0 ) {
            for ( Contact con : conList ) {
                con.Description = mapAccount.get(con.AccountId).Description;
            }
            update conList;
        } 
        
        caseList = [SELECT id,Description,ContactId,AccountId FROM Case WHERE ContactId IN:conList];
               
         if ( caseList.size() > 0 ) {
            for (Case caseObj : caseList ) {
                caseObj.Description = mapAccount.get(caseObj.AccountId).Description;
            }
            update caseList;
        }    
    }
}

Thank You
Ajay Dubedi
This was selected as the best answer
SFDC GuestSFDC Guest
Thank you both.