18

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

4
  • 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? Commented Feb 16, 2015 at 14:23
  • Not sure I understand what you mean? Do you mean by using custom RestApex? Commented Feb 17, 2015 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? Commented Feb 18, 2015 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. Commented Feb 19, 2015 at 18:12

4 Answers 4

11

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!

2
  • 2
    Is there a way to query potential duplicates for a given account without actually doing any data manipulation (i.e. without changing data)?
    – Marc
    Commented Nov 27, 2015 at 10:57
  • This can not be implemented via a REST API call/method as the DMLOptions are available only for the SOAP API. There's no way to force AllowSave = true on the DuplicateRuleHeader in a REST API call...
    – Koshera
    Commented Nov 19, 2020 at 19:57
5

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

3

There is an option available via REST API to set headers to bypass duplicate rule. Check here:

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/headers_duplicaterules.htm

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .