3

Please let me know how to perform exception handling in this scenario? Though we have added error message on child record we couldn't see it on the parent while the parent record is being updated (as we already know this). Is there any way to show the error messages of children of the parent (after update of parent updating childs). Else how we will handle these kind of scenarios? (I don't want to store error message in a log object). Thanks in advance.

public class AccountTriggerhelper {
    public static void updateAccounts(Map<Id,Account> newMap, Map<Id,Account> oldMap) {
        Set<Id> accountIdSet = new Set<Id>();
        List<Contact> contactsList;
        //Database.SaveResult[] lsr;

        for(Account acc :newMap.values()) {
            if(acc.Name != oldMap.get(acc.id).Name) {
                accountIdSet.add(acc.Id);
            }
        }

        if(accountIdSet.size()>0) {
            contactsList = [select id,name,Parent_Contact__c,Email from Contact Where AccountId In:accountIdSet];
        }

        for( Contact con :contactsList) {
            con.Parent_Contact__c = UserInfo.getUserId();
            con.Email = 'ttt@gmail.com';
            con.Phone = '5678765432';
        }

        if(contactsList.size()>0) {
                List<Database.SaveResult> lsr = Database.update(contactsList,false);

                for(Integer i=0;i<lsr.size();i++){
                    if (!lsr[i].isSuccess()){
                        Database.Error error = lsr.get(i).getErrors().get(0);
                        String failedDML = error.getMessage();
                        //contactsList[i].addError('Contact Insertion Failed: '+failedDML); 
                        Contact c= new Contact(id=contactsList[i].id);
                        c.addError('Contact Insertion Failed: '+failedDML);
                    }
                }
        }  
    }
}
3
  • 2
    Could you get the parent Account from the newMap by Id using the Contact.AccountId? Then do the addError on the parent account. Jul 2, 2015 at 0:15
  • Can you explain your question a little bit? So, you want to show error related to child object, onto parent object, when parent object records are inserted/ updated etc. ??
    – Anshul
    Jul 2, 2015 at 1:01
  • Yes @Anshul. We need to display the child error message on parent record. Jul 2, 2015 at 14:33

2 Answers 2

1

Daniel Ballinger has it.

I'm assuming this helper is called from an Account Trigger, so you don't want to addError to the contact, instead you want to add it to the associated Account.

if(accountIdSet.size()>0) {
           //Add Account Id to your query see ** below
            contactsList = [select id,name,Parent_Contact__c,Email,**AccountId** from Contact Where AccountId In:accountIdSet];
        }

        for( Contact con :contactsList) {
            con.Parent_Contact__c = UserInfo.getUserId();
            con.Email = 'ttt@gmail.com';
            con.Phone = '5678765432';
        }

        if(contactsList.size()>0) {
                List<Database.SaveResult> lsr = Database.update(contactsList,false);

                for(Integer i=0;i<lsr.size();i++){
                    if (!lsr[i].isSuccess()){
                        Database.Error error = lsr.get(i).getErrors().get(0);
                        String failedDML = error.getMessage();

                        //Using the AccountId from your query and the newMap of Accounts passed in from the trigger add the error to the Account in question.
                        **Id acctId = contactsList[i].AccountId;**
                        **newMap.get(acctId).addError('Contact Insertion Failed: '+failedDML);**
                    }
                }
        }  
2
  • This is not working.. Jul 2, 2015 at 14:37
  • Well I haven't tried to compile it, but the error does have to be added to the object which has the context - in this case the Account (at least I'm assuming the Account because of the name of the class). One shortcut (not as elegant) is to just use update list; and see at least the exception thrown is bubbling up. Jul 6, 2015 at 17:28
0

In the same way as Caleb Sidel and Daniel Ballinger. You need add in Contact query the field AccountId, and after you do the update, you need catch the contact error.

I prefer use a Map of Contacts vs List of Contacts, because SaveResult Class has the id of the success and wrong records.

public class AccountTriggerhelper {
public static void updateAccounts(Map<Id,Account> newMap, Map<Id,Account> oldMap) {
    Set<Id> accountIdSet = new Set<Id>();
    Map<String,Contact> contactById = new Map<String,Contact();
    //Database.SaveResult[] lsr;

    for(Account acc :newMap.values()) {
        if(acc.Name != oldMap.get(acc.id).Name) {
            accountIdSet.add(acc.Id);
        }
    }

    if(accountIdSet.size()>0) {
        contactById = new Map<String,Contact([select id,name,Parent_Contact__c,EmailAccountId from Contact Where AccountId In:accountIdSet]);
    }

    for( Contact con :contactById.values) {
        con.Parent_Contact__c = UserInfo.getUserId();
        con.Email = 'ttt@gmail.com';
        con.Phone = '5678765432';
    }

    if(contactsList.size()>0) {
            List<Database.SaveResult> lsr = ;

            Contact contactWithError;
            for(Database.SaveResult result : Database.update(contactsList,false)){
                if(!result.isSucces()){
                    contactWithError = contactById.get(result.getId());
                    String[] errors;
                    for(Database.Error er : result.getErrors()){
                        errors.add(er.getMessage());
                    }

                    newMap.get(contactWithError.AccountId).addError(String.join(errors,'. '));
                }
            }

    }  
}

}

Best Regards

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.