I'm trying to run the following unit test:

static void testInsertContactWithMultipleDuplicates() {
        TriggerManager.allTriggersDisabled = true;

        Contact dupRecord1 = new Contact(LastName = 'Pérez', Email = 'tiger@animals.com', AlternativeEmailAddress1__c = 'unicorn@animals.com');
        Contact dupRecord2 = new Contact(LastName = 'Serrano', Email = 'frog@animals.com', AlternativeEmailAddress2__c = 'unicorn@animals.com');
        insert new List<Contact> { dupRecord1, dupRecord2 };

        TriggerManager.allTriggersDisabled = false;

    Contact record = new Contact(LastName = 'García', Email = 'unicorn@animals.com');

    insert record;

The problem is the Test Runner compiler is throwing the following exception on the line in between the startTest and stopTest:

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, Use one of these records?: []

Obviously, that's the kind of exceptions that duplicate rules throw, but duplicate rules cannot compare different fields betweeen them (that is Email field with AlternativeEmail field), so I'm kind of lost here.

I do have a duplicate rule created like the below, but it doesn't make sense for that one to trigger, given the test data. What am I missing here?

enter image description here

Just to explain the rule, its "access conditions" make sure that the contact has at least an email and detects them as duplicates based on the EmailSetId, which is custom field through which we group the records. If two contacts are considered duplicates, they are assigned the same EmailSetId. That's populated in the before trigger of contacts and leads.

  • 1
    Can you please tell, what is EmailSetId field which you have used in your matching criteria? It might help to find cause of your probelm
    – sfdc_sk
    Apr 4 '18 at 14:23
  • @sfdc_sk Added an explanation in the question :) Apr 4 '18 at 14:29
  • Okay. The field EmailSetId is populated using Triggers and when you consider the two contacts are duplicate? what is matching criteria you are using in triggers?
    – sfdc_sk
    Apr 4 '18 at 14:34
  • @JavierGarcíaManzano based on your explanation -- "If two contacts are considered duplicates, they are assigned the same EmailSetId." -- does the trigger logic look for existing contacts and set the EmailSetId based on the email of the incoming record as well as any existing record?
    – Jayant Das
    Apr 4 '18 at 14:38
  • @JayantDas Ugh, you just made me realize my error... Of course. The trigger populates that in the before and then, since in the order of execution duplicate rules come after, then they detect them. My bad. You can submit an answer if you want and I'll accept it. Thanks! Apr 4 '18 at 14:42

Based on your explanation:

"If two contacts are considered duplicates, they are assigned the same EmailSetId."

If the before trigger logic looks for existing contacts and sets the EmailSetId based on the emails of the incoming records as well as any existing records, then the duplicate rule is marking those records while you are inserting them and that is the possible cause of the error.

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.