15

So we are experimenting with the new duplicate matching rules in salesforce which seems to work great. My question is if its possible to use the more advanced features when working with data over the Rest API

So right now if you have a duplicate rule setup and you try to create record via the api it correctly errors and send you back the error message text as expected.

But if you say "allow" but "alert the user" it also throws an error when trying to save via the rest api.

So specifically is there a way to get the list of "possible matches" and related to that, how can we "Save (ignore alert)". Id be curious if anyone knows how we might implement something like this either using apex or just the rest api

any insight you might have would be helpful.

enter image description here

UPDATE:

So i was able to find this for how to do it in apex here: is there similar functionality (ie. AllowSave = true) via the api

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_Database_DuplicateError.htm

  • I think it should be possible to have the code to allow duplicates in your REST Webservice which will allow to insert the duplicates. Is there any restriction for the same? – RitikaBhrgv Feb 16 '15 at 14:23
  • Not sure I understand what you mean? Do you mean by using custom RestApex? – Keith Mancuso Feb 17 '15 at 21:26
  • Yes, I meant in apex code in Rest Api, we can get the duplicate record ids as well using the standard methods. Do you need the same functionality in standard salesforce UI also? – RitikaBhrgv Feb 18 '15 at 3:30
  • Hi, I have added an answer since haven't received your response for above comment. Let me know if this works for you. – RitikaBhrgv Feb 19 '15 at 18:12
10

The link which you have shared allows the user to bypass the duplicate matching rule defined in salesforce. In REST API, the method will be same. Sharing a code snippet here for enforcing the insertion of duplicate records and fetching the already existing duplicate records.

The method simply creates an Account and returns the list of duplicate accounts existing in the database.

@HttpPost
global static List<Id> fetchDuplicates(String name, String phone, String website) {

    Set<Id> setDuplicateIds = new Set<id>();
    Account account = new Account(Name = name, phone = phone, website = website);
    Database.SaveResult sr = Database.insert(account, false);

    if (!sr.isSuccess()) {

        Datacloud.DuplicateResult duplicateResult;

        // Insertion failed due to duplicate detected
        for(Database.Error duplicateError : sr.getErrors()){

            duplicateResult = ((Database.DuplicateError)duplicateError).getDuplicateResult();
        }

    // Fetch the Ids of the existing duplicate records
        for(Datacloud.MatchResult duplicateMatchResult : duplicateResult.getMatchResults()) {

            for(Datacloud.MatchRecord duplicateMatchRecord : duplicateMatchResult.getMatchRecords()) {

                setDuplicateIds.add(duplicateMatchRecord.getRecord().Id);
            }
        }

        System.debug('Duplicate records ' + setDuplicateIds);

        // If the duplicate rule is an alert rule, we can try to bypass it
        Database.DMLOptions dml = new Database.DMLOptions(); 
        dml.DuplicateRuleHeader.AllowSave = true;
        Database.SaveResult sr2 = Database.insert(account, dml);

        if (sr2.isSuccess()) {
            System.debug('Duplicate account has been inserted in Salesforce!');
        }
    }

// Return the existing duplicate records Ids
    List<Id> lstDuplicateIds = new List<Id>();
    lstDuplicateIds.addAll(setDuplicateIds);
    return lstDuplicateIds;
}

Hope this helps!

  • 1
    Is there a way to query potential duplicates for a given account without actually doing any data manipulation (i.e. without changing data)? – Marc Nov 27 '15 at 10:57
3

My answer here is the same one I gave on Is it possible to leverage SFDC Duplicate Matching fuzzy algorithm's within APEX code?

But for posterity, I figured it should be marked as the correct answer for this question too:

Winter '18 promises to bring the findDuplicates() method to Apex -- this has been available to the SOAP API for some time but now you can access the list of duplicate candidates in Apex.

https://developer.salesforce.com/docs/atlas.en-us.210.0.apexcode.meta/apexcode/apex_class_Datacloud_FindDuplicates.htm#apex_class_Datacloud_FindDuplicates

There were some previous 'workarounds' that involved trying to INSERT a record and inspect the Exception class to see if it was rejected due to a Duplicate Rule being violated. The findDuplicates() method is far cleaner

0

There is no such functionality in Salesforce REST API.

Either You can create Your own REST API method with use of APEX (like example presented in RitikaBhrgv's answer)

or You may want to use SOAP API that supports DuplicateRuleHeader.

See details here: https://developer.salesforce.com/forums/?id=906F00000005LILIA2

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.