0 votes
1 view
in Salesforce by (11.9k points)

I am working on a module which will de-duplicate contact records on insert/update but I am hitting into Apex CPU time limit exceeded error. I understand that to overcome it we need to optimize the code a little but in the following code block, there seems very little scope of optimizing it. Any help will be appreciated.

On Account, we have a multi picklist from where we can select the fields based on which the uniqueness of the contacts under this account will be defined. It can be different for a different account. The following code is a part of a handler class for trigger in which we have a map of old contact list with account Id as key (mapOfAccountIdWithItsContact) and a map with new contact list and account as key (newContactWithAccountMap) we iterate over these maps based on set of account whose contacts we have to get in trigger. We have a map to store List of fields to be used for contact uniqueness for each account (mapOfAccountWithFilters).

Here is the code snippet:

for(String accountId : accountIdSet){

        if(newContactWithAccountMap.get(accountId) != null){

            for(Contact newContact : newContactWithAccountMap.get(accountId)){

                for(Contact oldContact : mapOfAccountIdWithItsContact.get(accountId)){

                    //Check for duplication only in the respective account, also this should not apply on insertion of Office contact

                    matchingContactFound = false;

                    if(oldContact.id != newContact.id){  //while insert, newContact's id will be null and while update it will verify that it is not matching itself with its old record. 

                        for(String filterFieldName : mapOfAccountWithFilters.get(accountId)){

                            if(oldContact.get(filterFieldName) == newContact.get(filterFieldName)){

                                matchingContactFound = true;

                                //If match is found update last de duplication date to today on old contact

                                oldContact.Last_De_Duplication_Date__c = System.Today();

                                oldContactsToUpdateSet.add(oldContact);

                            }else{

                                matchingContactFound = false;

                                break; //get another "old contact"

                            }

                        }

                    }

                    if(matchingContactFound){                               

                        //stop it from being inserted

                        duplicateContactSet.add(newContact.Id);

                        //newContact.addError('Contact cannot be inserted because a contact is already present based on the Master Target Identifier at client level.');

                        break; //get another "new contact"

                    }                       

                }

            }

        }

    }  

Any help in avoiding 4 loops or an alternate approach will be greatly appreciated. Thanks in advance.

1 Answer

0 votes
by (31.6k points)

So there are the following things I'm concerned about:

  1. How did you narrow it down to this particular snippet, have you made any timing tests?
  2. Maybe you're querying for too much data, maybe some smarter filtering upfront could greatly reduce the execution time... For example, you have there a comment about this should not apply on insertion of Office contact - do you already filter this out before you even enter this method?
  3. How do you populate your variables and of what type are they (I prefer to see Map<Id, List<Contact> etc then try to figure it out from the description)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...