0

I am writing the trigger for When I update the contact record description value of an Account, and if other contacts description value related to that Account is matched. Then update the Account description value with the contact description.

Example: There is one Account record: Account1 There are 3 Contact records for this Account: Contact1, Contact2, Contact3.

If I update Contact1 description as 'test desc', trigger should check if remaining contacts Contact2 and Contact3 are having same description value, if yes please update Account description with Contact1 description.

How to check whether contacts also have same description of same account in trigger. please help. Thanks in advance.

Below trigger works if we update the contact descritpion then Account description will be updated.

trigger UpdateDescription on Contact (after insert, after update) { 
    Map<ID, Account> parentAccts = new Map<ID, Account>(); 
    List<Id> listAccIds = new List<Id>();

    for (Contact contObj : Trigger.new) {
        listAccIds.add(contObj.AccountId);
    }

    parentAccts = new Map<Id, Account>([SELECT id, Description,(SELECT ID, Description FROM Contacts) FROM Account WHERE ID IN :listAccIds]);

    for (Contact con: Trigger.new){
        Account myParentAcc = parentAccts.get(con.AccountId);
        myParentAcc.Description = con.Description;
    }

    update parentAccts.values();
}
1

You logic should query related contacts and check if all related contacts have the same Description and after that update account Description and send this account to update with new description

trigger UpdateDescription on Contact (after insert, after update) {
    Set<Id> acctsIds = new Set<Id>();
    for(Contact cont : Trigger.new){
        acctsIds.add(cont.AccountId); // collect parent account ids
    }
    acctsIds.remove(null); // remove from ids null. because it is possible to create Contact not related to Account 

    //query parent accounts with related already existed contacts
    Map<Id, Account> parentAccts = new Map<Id, Account>([
        select Id, Description,(
            select Id, Description
            from Contacts
            )
        from Account
        where Id in :acctsIds
    ]);

    //prepare container for accounts to update
    List<Account> acctsToUpdate = new List<Account>();

    //iterate contacts in trigger context
    for(Contact cont :Trigger.new){
        Account parentAcct = parentAccts.get(cont.AccountId);
        //make flag that determines if all related conatcts have the same description
        Boolean allSameContDescription = true;
        for(Contact existedRelatedCont :parentAcct.Contacts){
            // if description is not the same break the loop
            if(cont.Description != existedRelatedCont.Description){
                allSameContDescription = false;
                break;
            }
        }
        // if for all related contacts description is the same - update description on parent account
        if(allSameContDescription){
            parentAcct.Description = cont.Description;
            acctsToUpdate.add(parentAcct);
        }
    }
    update acctsToUpdate;
}
| improve this answer | |
  • @SFDC as for me, this is what we have to have. Maybe there could be other implementations of this logic :) you could always create service class to split it into small service methods – Oleksandr Berehovskyi Oct 25 '18 at 7:25
0

Prepare a set of description inside the loop and then check we only have one count in that set and description is matched with current contact then update account description. Like that:

 for (Contact con: Trigger.new) {
    Account myParentAcc = parentAccts.get(con.AccountId);
    List<contact> contacts = myParentAcc.Contacts;
    Set<string> contactDescriptions = new Set<string>();
    for(contact c : contacts)
    {
       if(c.Description != null)
           contactDescriptions.add(c.Description.trim());
    }
    if(contactDescriptions.size() == 1 && contactDescriptions.contains(con.Description))
    {
       myParentAcc.Description = con.Description;
    }
}
| improve this answer | |
  • Getting error as: Error: Compile Error: Expression must be a list type: Set<String> at line 19 column 63 – SFDC Oct 25 '18 at 7:19
  • at which line? I think I updated that one. – Pragati Jain Oct 25 '18 at 7:20
  • @SFDC, have you tried above solution? – Pragati Jain Oct 25 '18 at 8:42
  • yes. tried but its throwing error at if(contactDescriptions.size() == 1 && contactDescriptions[0]==con.Description). Error as: Error: Compile Error: Expression must be a list type: Set<String> at line 20 column 63 – SFDC Oct 25 '18 at 8:56
  • Try now with updated one. – Pragati Jain Oct 25 '18 at 9:03

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.