2

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;
    }
}
  • Can you explain whats the issue you are facing? – Santanu Boral Aug 3 '18 at 6:53
  • It's working fine for insertion. But I need it for update operation. – SFDC Aug 3 '18 at 7:00
  • did you write a code for updating? – Santanu Boral Aug 3 '18 at 7:20
  • I wrote for updating Contact description in above code. I need to update Contact related Cases as well. – SFDC Aug 3 '18 at 10:38
0
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,(select id, Accountid,Contactid, Description from Cases),
                   AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
    List<Case> casetoupdate= new List<Case>();
    if ( listContact.size() > 0 ) {
        for ( Contact con : listContact ) {
            con.Description = mapAccount.get(con.AccountId).Description;
            for(Case cas:con.Cases){
                cas.Description=con.Description;
                casetoupdate.add(cas);
            }
        }
        update listContact;
        update casetoupdate;
    }
}

This Will achieve your requirement.After updating the account description, your contact and cases related to those contact will get updated.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.