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;
    }
}
4
  • Can you explain whats the issue you are facing? Aug 3, 2018 at 6:53
  • It's working fine for insertion. But I need it for update operation.
    – SFDC
    Aug 3, 2018 at 7:00
  • did you write a code for updating? Aug 3, 2018 at 7:20
  • I wrote for updating Contact description in above code. I need to update Contact related Cases as well.
    – SFDC
    Aug 3, 2018 at 10:38

2 Answers 2

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.

1
-1
if(trigger.isupdate)
{
    list<account> lstacc = new list<account>();
    set<id> setAccids = new set<id>();
    for(contact opp : trigger.new)
    {
        setAccids.add(opp.AccountId); 
    }
    map<id,Account> mapAcc = new  map<id,Account> ([select id ,Description from account where id =:setAccids]);
    for(contact opp: trigger.new)
    {
        if(trigger.isinsert || trigger.isupdate && trigger.OldMap.get(opp.Id).Description != opp.Description)
        {
            Account objAcc = mapAcc.get(opp.AccountId);
            objacc.Description =  opp.Description;
            lstacc.add(objacc); 
        }
    }
    update lstacc;      
}
1
  • Welcome to Salesforce Stack Exchange (SFSE)! This is an answer to an almost 4-year-old question with an already accepted almost 4-year-old answer. While it is acceptable to add answers to old questions, it should only be when and where appropriate - say, for example, SF adds new functionality. But your answer does not even address the original question which states, "But I need to update Case records of Contact as well." Please edit your answer to address that question (and then explain how your answer is different or better). Or maybe just delete this answer & contribute elsewhere on SFSE.
    – Moonpie
    Jun 14, 2022 at 15:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .