From Ryan Mercer: A better way to Check for Duplicate Records in Flow

You probably have Duplicate Rules configured in your Salesforce org, right? Well, if you don’t, you should check them out!

A duplicate rule defines what happens when a user views a record with duplicates or starts creating a duplicate record. Salesforce provides standard duplicate rules for business and person accounts, contacts, and leads. You can also create duplicate rules.

Duplicate rules can bring your Flow to a screeching halt if you try to create a record that already exists. Your duplicate rule is working exactly as designed; your flow, however, is not.

Screenshot of a Flow error message that reads - 'Error Occurred: This error occurred when the flow tried to create records: DUPLICATES_DETECTED: Use one of these records?. You can look up ExceptionCode values in the SOAP API Developer Guide.'

Introducing “Duplicate Record Check”

Duplicate Record Check is an Apex Invocable Action that will allow you to run your duplicate rules on records prior to creating them in your Flow. Take a look at this video introduction for an explanation as to why and how you might use this action.

How to Configure the Component

You can either pass in a single record, a collection of records, or both into the action. If using both, the single record and collection of records MUST be of the same object type!

The action will return two Apex-Defined variables of the Duplicate class:

A screenshot that shows there are two returns from the action: 1. single input return and 2. collection input return.

And each variable, will include:

  1. Whether a duplicate was found;
  2. The duplicate record’s Id; and
  3. The duplicate record’s Object name.
And each returned object has three data points: 1. Boolean identifying whether there's a duplicate, 2. the Duplicate record Id, and 3. the Duplicate record object name.

Apex Developer Support

Don’t write Apex? Feel free to skip over this!

This component is supported by the Datacloud Namespace. The shape of the Datacloud results object is a little unwieldy. You can use the DuplicateRecordCheck_Util class to make your life a little easier. Here’s how you use it:

// Assuming your Duplicate Rules are configured accordingly.
Lead ld = new Lead(
  FirstName = 'Ryan',
  LastName = 'Mercer',
  Email = 'ryangmercer@gmail.com'
  Company = 'unofficialsf'
);
insert ld;
        
sObject input = ld.clone(false,true);
List<Duplicate> duplicates = DuplicateRecordCheck_Util.findDuplicates(new List<sObject>{input});
        
system.assert(duplicates[0].isDuplicate == TRUE);
system.assert(duplicates[0].duplicateRecordId == ld.Id);
system.assert(duplicates[0].duplicatesObjectType == 'Lead');

Installation

Production or Developer Version 1.0

Sandbox Version 1.0


View Source

View Source