1

I am stuck when trying to look for a duplicate contact, I am using Datacloud.FindDuplicatesResult, however the system is not getting any duplicate so when it tries to save the record I get an:

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED

Below is my code on how i am trying to use Datacloud.FindDuplicatesResult, but i dont understand what I am missing?

This is result of results (Datacloud.FindDuplicatesResult[]):

(Datacloud.FindDuplicatesResult[getDuplicateResults=(Datacloud.DuplicateResult[getDuplicateRule=Find_Contacts;getDuplicateRuleEntityType=Contact;getErrorMessage=null;getMatchResults=(Datacloud.MatchResult[getEntityType=Contact;getErrors=();getMatchEngine=ExactMatchEngine;getMatchRecords=();getRule=New_Contact_v3;getSize=0;isSuccess=true;]);isAllowSave=false;]);getErrors=();isSuccess=true;])

//CHECK DUPLICATE 
Account myAcc = new Account();
Opportunity myOpp = new Opportunity();

List<Contact> myContactList = new List<Contact>();          
Contact myCon = new Contact(
    FirstName = obj.billing_address.first_name,
    LastName = obj.billing_address.last_name,
    Email = obj.email,
    MobilePhone = obj.phone
);

//MANAGE DUPLICATE FOR CONTACT
myContactList.add(myCon);
Datacloud.FindDuplicatesResult[] results = Datacloud.FindDuplicates.findDuplicates(myContactList);

for (Datacloud.FindDuplicatesResult findDupeResult : results) {
    for (Datacloud.DuplicateResult dupeResult : findDupeResult.getDuplicateResults()) {
        for (Datacloud.MatchResult matchResult : dupeResult.getMatchResults()) {
            for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
                if (matchRecord.getMatchConfidence() >= 90) {
                    myCon = (Contact)matchRecord.getRecord();
                    myAcc = myCon.Account;
                    break;
                }
            }
        }
    }
}

myCon.MailingStreet = obj.billing_address.address1;
myCon.MailingCity = obj.billing_address.city;
myCon.MailingState = obj.billing_address.province_code;
myCon.MailingCountry = obj.billing_address.country;
myCon.MailingPostalCode = obj.billing_address.zip;
if (myCon.Id == null) {
    myCon.AccountId = myAcc.Id;
    insert myCon;
} else {
    update myCon;
}

Is there anything I am missing?

0

So I finally found what was going on, my duplicate rule use 5 fields

  1. First Name
  2. Last Name
  3. Email
  4. Mobile
  5. Mailing Zip/Postal Code

When I created the contact to find the duplicate i didnt have the mailing zip:

List<Contact> myContactList = new List<Contact>();          
Contact myCon = new Contact(
    FirstName = obj.billing_address.first_name,
    LastName = obj.billing_address.last_name,
    Email = obj.email,
    MobilePhone = obj.phone
);

So I just added

MailingPostalCode = obj.billing_address.zip,

And thats fixed

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.