1

I'm replacing a formula field (due to hitting the max number of 15 object references) with a trigger on Account (after insert/update) and on Contact (before insert/update). There are three account lookups on a Contact each with two CC email fields that need to be concatenated together and saved to CC for Accounts (text). The Contact trigger will check if one of the three lookups is changing and will update the CC for Accounts field. This is working correctly.

The issue I'm running into is on the Account trigger. It should check if the CC_Confirmed_By__c (text) is changing and then query the Account and related Contacts. It should then check for values in the two cc emails on the three Accounts and concatenate that into one string.

It will update the first contact with the correct string, however, any other contacts will have inconsistent results like an cc email from an unrelated Account lookup. I've tried using a List<String> to collect the cc emails and a Map<Id,List<String>> but the results are never correct. The example below is one of several versions I've tried with no success.

Also, a validation rule prevents two (AFMO and Agency) from being saved to the record at the same time. There is also a workflow that will update the CC_Confirmed_By__c (text) to blank if either of the CC emails change. I feel like the obvious is staring me in the face, but I'm just not seeing it. Any help will be appreciated.

Set<Id> accountSet = new Set<Id>();
List<Account> accountList = (List<Account>) Trigger.new;

        for (Account account : accountList){
            if (trigger.isInsert){
                if (!String.isEmpty(account.CC_Confirmed_By__c)){
                    if(account.RecordTypeId == AccountRecordTypes.get('AFMO_Account_Record_Type') || account.RecordTypeId == AccountRecordTypes.get('Agency_Account_Record_Type') 
                        || account.RecordTypeId == AccountRecordTypes.get('Broker_Dealer_Account_Record_Type')){
                        accountSet.add(account.Id);
                    }
                }
            } else if (trigger.isUpdate){
                if (Utils.isFieldChanging('CC_Confirmed_By__c', account, Trigger.oldMap) && !String.isEmpty(account.CC_Confirmed_By__c)){
                    if(account.RecordTypeId == AccountRecordTypes.get('AFMO_Account_Record_Type') || account.RecordTypeId == AccountRecordTypes.get('Agency_Account_Record_Type') 
                        || account.RecordTypeId == AccountRecordTypes.get('Broker_Dealer_Account_Record_Type')){
                        accountSet.add(account.Id);
                    }
                }
            }
        }

        if (accountSet.size() > 0){
            Map<Id, Account> accountMap = new Map<Id, Account>(
                    [SELECT Id, CC_Email_AFMO_1__c, CC_Email_AFMO_2__c, (SELECT Id, CC_for_Accounts__c, AFMO_Account__c, Agency__c, Broker_Dealer__c, 
                        AFMO_Account__r.CC_Email_AFMO_1__c, AFMO_Account__r.CC_Email_AFMO_2__c, Agency__r.CC_Email_AFMO_1__c, 
                        Agency__r.CC_Email_AFMO_2__c, Broker_Dealer__r.CC_Email_AFMO_1__c, Broker_Dealer__r.CC_Email_AFMO_2__c FROM Contacts) 
                        FROM Account WHERE Id IN :accountSet]);
            String ccForAccounts = '';
            List<String> ccList = new List<String>();
            List<Contact> contactsToUpdate = new List<Contact>();
            for (Account account : accountList){
                if (accountMap.containsKey(account.Id)){
                    Account acct = accountMap.get(account.Id);
                    List<Contact> contactList = acct.Contacts;
                    for (Contact c : contactList){
                        if (!String.isBlank(c.AFMO_Account__r.CC_Email_AFMO_1__c)){
                            ccForAccounts = ccForAccounts + c.AFMO_Account__r.CC_Email_AFMO_1__c + '; ';
                        }
                        if (!String.isBlank(c.AFMO_Account__r.CC_Email_AFMO_2__c)){
                            ccForAccounts = ccForAccounts + c.AFMO_Account__r.CC_Email_AFMO_2__c + '; ';
                        }
                        if (!String.isBlank(c.Agency__r.CC_Email_AFMO_1__c)){
                            ccForAccounts = ccForAccounts + c.Agency__r.CC_Email_AFMO_1__c + '; ';
                        }
                        if (!String.isBlank(c.Agency__r.CC_Email_AFMO_2__c)){
                            ccForAccounts = ccForAccounts + c.Agency__r.CC_Email_AFMO_2__c + '; ';
                        }
                        if (!String.isBlank(c.Broker_Dealer__r.CC_Email_AFMO_1__c)){
                            ccForAccounts = ccForAccounts + c.Broker_Dealer__r.CC_Email_AFMO_1__c + '; ';
                        }
                        if (!String.isBlank(c.Broker_Dealer__r.CC_Email_AFMO_2__c)){
                            ccForAccounts = ccForAccounts + c.Broker_Dealer__r.CC_Email_AFMO_2__c + '; ';
                        }
                        c.CC_for_Accounts__c = ccForAccounts;
                        contactsToUpdate.add(c);
                    }
                }
            }
            if (!contactsToUpdate.isEmpty()){
                update contactsToUpdate;
            }
}
}```

1 Answer 1

0

You need to clear/reset the ccForAccounts variable before each new interation of the Contacts loop. Or rather just declare it inside the Accounts loop. You just keep adding to it all the time.

You must log in to answer this question.

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