This post was written by Salesforce Consultant Ryan Mercer. It was last updated on 12/01/21.

Sign up for our newsletter!
A version of this post was sent out in the September '21 Kicksaw email newsletter. To make sure you get access to content like this in advance, sign up to get our monthly newsletter in your inbox!

An Invocable Method for Managing Declarative Duplicates

We've all been there: while quickly trying to add a Primary Contact to an Opportunity, we realize there are four different Jack Rogers contact records associated with the Burlington Textiles Corp of America account. Ugh.

Duplicate management is a super important part of your data plan, and helps to avoid this 👆 kind of nonsense. If you aren't using duplicate and matching rules, you need to hop on the band wagon STAT. There's plenty of great reading out there on the best practices of setting up functional duplicate rules for your business — that's NOT what this article in about. I'm here to show you a nifty little invocable method I wrote to help manage duplicate data ... in flows!

Have you ever received this error in your flow debugs? 🙋‍♂️

Error element Create_Contact (FlowRecordCreate).This error occurred when the flow tried to create records: DUPLICATES_DETECTED: Use one of these records?

One of your Users just tried to create a duplicate record (boo), but was stopped by your brilliantly designed duplicate rules (yay!). But in doing so, your beautiful flow came crashing to an end (boo!). You might work around this by adding a GET to find potential dupes; if any are found, don't CREATE. That can get messy, especially if you have to check multiple objects (I'm looking at you Leads and Contacts). 👀

'OR's make me cringe.

Those of us brave admins who have ventured down this path know that this only leads to heartbreak. A multitude of exceptions are going to squeak by, and don't get us started on if the duplicate rules change down the road (which they will). Can't we just ask Salesforce, "Hey, I've got this Contact that I haven't created yet, does it already exist?" Why yes, yes we can!

After doing some hack-y things over the years, I decided to solve this problem once and for all with an invocable method:

Don't be overwhelmed — it's easy to use! Just pass in a record variable (or even just a recordId) and the code will spit out whether or not there is a duplicate record according to the duplicate rule configuration! If a duplicate record exists, then it will also give you the duplicate's recordId.

How do you use it?

Let's say you already have this Lead.

You created an awesome screen flow to help Users enter Contacts quicker. One of your Users inadvertently tries to add Ryan Mercer the Wizard again (as a Contact). Watch what happens.

Example flow design

Obligatory finished product first pic.

Step One - Input screen

Step Two - Assign the input values to the varContact record variable

Step Three - Stir in the magic sauce

  1. Drop in an Action element onto the canvas.
  2. Type "Check for Duplicate Records" in the search bar.
  3. In this case, the Object for Record to Check for Duplicates is Contact.
  4. In this case, don't include Record Id to Check for Duplicates (you should be able to find an interesting use case for this after experimenting a bit).
  5. Include Record to Check for Duplicates by selecting {!varContact}.
  6. Under Advanced, be sure to Manually assign variables:
  • Duplicate Record Id = {!varDuplicateRecordId} a text variable
  • Duplicate sObject Type = {!varDuplicatesObjectType} a text variable
  • Is there a Duplicate = {!isDuplicate} a boolean variable

Step Four and on - Now we can leverage the return output variables to perform advanced logic

If NO, Create Contact.

If YES:

GET either the Lead or Contact using {!varDuplicateRecordId}.

And then go to town!

Want the Apex test class so that you can deploy to production? Connect with Ryan on LinkedIn and send me a message. Hope to hear from you!

Explore another topic:

Contact Us

Thank you! Your submission has been received!

Oops! Something went wrong while submitting the form