[SalesForce] Duplicate Matching Rules via rest API

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

Best Answer

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!

Related Topic