0

I'm new to salesforce and am unsure how to approach this error message. I had created this class that rolls down a Contact Role from a Contact created on a Parent to its children and a trigger that executes on creation. It had worked yesterday but when testing it again I now recieve this error message:

Validation Errors While Saving Record(s)

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger LAM_Contact_ReleatedList caused an unexpected exception, contact your administrator: LAM_Contact_ReleatedList: execution of AfterUpdate caused by: System.DmlException: Upsert failed. First exception on row 0; first error: DUPLICATE_VALUE, Contact has already been added in that Contact Role: [ContactId, Role]: Class.LAM_RelatedListUpdate.RollContactRolesToAccountChildren: line 36, column 1".

Would someone be able to explain to me what changed and what to look at to fix this issue? thank you.

Apex Code:

public class LAM_RelatedListUpdate {
    Public static void RollContactRolesToAccountChildren(List<Contact> con){
        //variables for the account lam record types
         Id LAMCRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Customer Account').getRecordTypeId();//get recordtypeID of Large Customer
         Id LAMSRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Site Account').getRecordTypeId();//get recordtypeID of Large Customer
         Id LAMDRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Division Account').getRecordTypeId() ;//get recordtypeID of Large Customer


        //list of child accounts, parent accounts, contacts and contact roles
        List<Id> accountsFromContacts = new List<Id>();  
        List<Account> parentAccounts = new List<Account>();
        List<Account> listOfAccountChildren = new List<Account>();
        List<AccountContactRole> contactRolesClones = new List<AccountContactRole>();

        System.debug(con);

        for(contact c : con){
            accountsFromContacts.add(c.AccountId);
        }

        parentAccounts = [Select Id, RecordTypeId from Account Where Id = :accountsFromContacts ];


        listOfAccountChildren = [Select Id, RecordTypeId, Parent_Account_for_LAM__c from Account Where Parent_Account_for_LAM__c = :accountsFromContacts ];
        //updates the contact roles of the child accounts
        for(contact c : con){
            for(Account a : listOfAccountChildren ){
                if(isMatchingRecord(a.Parent_Account_for_LAM__c, parentAccounts) ){
                    AccountContactRole cloneConatactRole = new AccountContactRole( AccountId = a.id , ContactId = c.Id,  Role = c.Customer_Role__c  );
                    contactRolesClones.add(cloneConatactRole);
                }
            }
        }


        upsert contactRolesClones;

    }
    //checks that the parent account is of these lam record types, and if the parentId matches the one of the child
    public static Boolean isMatchingRecord(Id ParAcc, List<Account> acc){
         Id LAMCRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Customer Account').getRecordTypeId();//get recordtypeID of Large Customer
         Id LAMSRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Site Account').getRecordTypeId();//get recordtypeID of Large Customer
         Id LAMDRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Division Account').getRecordTypeId() ;//get recordtypeID of Large Customer
        for (account a: acc){
            if (a.Id == ParAcc ){
                if(a.recordtypeid == LAMCRecordTypeId  ||  a.recordtypeid == LAMDRecordTypeId || a.recordtypeid == LAMSRecordTypeId){
                    return true;
                }
            }
        }

        return false;
    }
}

Trigger:

trigger LAM_Contact_ReleatedList on Contact (after insert, after update) {
    LAM_RelatedListUpdate.RollContactRolesToAccountChildren(trigger.new);
}
0

There are two ways you can handle this scenarios:

  1. Verify if the contact is already exists in the contact role and if so then eliminate those before putting into the AccountContactRole list.

  2. Use Database.insert(recordToInsert, allOrNone) and Database.update(recordToUpdate, allOrNone) method with allOrNone=false, so that if any error occurs whole execution will not get stopped.

Put appropriate Trigger.addError(message) to show the message along with the above.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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