0

I'm trying to insert duplicate contacts inside this test method to cause a DuplicateError, however the call to Datebase.insert(duplicateContact, false) succeeds without errors. I want the error. I'm following this page in the docs: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Database_DuplicateError.htm. What am I doing wrong here?

@isTest
public static void testDuplicate() {
    Test.StartTest();

    Contact contact = new Contact();
    contact.firstname = 'dupe';
    contact.lastname = 'dupe';
    contact.email = 'test@test.com';
    insert contact;

    Contact duplicateContact = new Contact(firstname='dupe', lastname='dupe', email='test@test.com');
    Database.SaveResult result = Database.insert(duplicateContact, false);

    for(Database.Error error : result.getErrors()) {
        if (error instanceof Database.DuplicateError) {
          // do something here.. 
        }
    }

    Test.stopTest();
}
  • Hi, are you enabled duplicate management ? Or any trigger written to find duplicate record ? – Prem Anandh Aug 8 '18 at 3:57
  • @PremAnandh Enable duplicate management.. hmm. I don't know. How is that done with Apex? – shmuli Aug 8 '18 at 3:59
  • Okay. You can find two way whether the record is duplicate or not. 1. By using Trigger and Duplicate management by using configuration. Then only your logic will work here, without any performance Salesforce will not find dulicate records. – Prem Anandh Aug 8 '18 at 4:00
  • So both records get created in system? Check you might getting some other error – Tushar Sharma Aug 8 '18 at 4:04
  • @TusharSharma Both records are created. No errors. – shmuli Aug 8 '18 at 4:12
0

As per your clarification, you have to write trigger or duplicate management configuration to find the contact record is duplicate or not.

As of now Salesforce will not throw duplicate error automatically until you perform duplicate operation.

You can use Name or email Id or Phone filed to find duplicate records( depends on your requirement)

  • This is precisely what I'm trying to do, but with the error: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – shmuli Aug 8 '18 at 4:09
  • Create duplicate management and add conditions based on your requirement and activate it. Once you done this create two contact records with same details. – Prem Anandh Aug 8 '18 at 4:13
  • I can't use the declarative interface, I need to do this from Apex. It's for a package. Conditions may vary instance to instance, and I need this test to pass regardless of the environment. – shmuli Aug 8 '18 at 4:16
  • Then you have to write for Apex trigger to find duplicate. – Prem Anandh Aug 8 '18 at 4:30
  • I didn't understand, Do you want to write duplicate rule via apex? – Prem Anandh Aug 8 '18 at 4:42
0

You are missing some important steps while using that code. As per docs

Contains information about an error that occurred when an attempt was made to save a duplicate record. Use if your organization has set up duplicate rules, which are part of the Duplicate Management feature.

You first need to setup duplicate rules to use this class. As of now it doesn't look like you have setup this that why you are not getting proper error messages. As of now, Salesforce doesn't prevent duplicates unless you enable duplicate rules.

You can also follow this trailhead: Resolve and Prevent Duplicate Data

You can also use trigger to setup this. But that's not an easy part in your use case as you have multiple orgs with different conditions. So I suggest you check duplicate rules they will surely help you.

  • Standard contact matching rules are activated by default. help.salesforce.com/…. If I understand correctly, this should result with a DuplicateError due to the OR (First Name AND Last Name AND Email) rule. – shmuli Aug 8 '18 at 4:37
  • @shmuli they are not activated they are just created. You can use them but first, you need to enable them. – Tushar Sharma Aug 8 '18 at 4:38
  • The docs read: "The standard contact [..] matching rules identify duplicate contacts [..]. They’re activated by default." Also, I'm looking for a way to do this with Apex. – shmuli Aug 8 '18 at 4:41
  • @shmuli then verify someone might disabled them. To do this in apex you need to write your own logic. This will help you salesforce.stackexchange.com/questions/21912/… – Tushar Sharma Aug 8 '18 at 5:00
  • A trigger isn't gonna do the trick. I specifically need the DuplicateError, since I'll be using it for this: Datacloud.DuplicateResult duplicateResult = ((Database.DuplicateError)duplicateError).getDuplicateResult(); . – shmuli Aug 8 '18 at 5:13

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.