Preventing duplicate junction object records

Native dupe management only allows you to reference a single lookup field. So it cannot prevent duplicate junction object records.

By using a (new in spring 20) before save flow to concatenate the two IDs into a single text field, you can prevent duplicate junction object records.

This cannot be done with workflow rules, because of the order of operations. Dupe rules are evaluated at step 6, and workflow at step 11 (as of Feb 2020)

Here are the steps

  1. create a text field named Compound Key on your junction object
  2. create a flow
  3. click on the start button and set the flow to launch on ‘new or updated records’ (you can decide if it fires on insert, update, or both)
  4. add an assignment element that sets Compound Key to a formula field which concatenates the two ID fields on the record
  5. create and activate a matching rule that matches on Compound Key (exact)
  6. create and activate a dupe rule that alerts / blocks - whatever makes sense in your case

Moredetails and screenshots below

the whole two-step flow (!)
Screenshot 2020-02-08 at 7.31.21 AM.png

step 3 : after clicking on start button
Screenshot 2020-02-08 at 7.28.36 AM.png

step 4: the assignment element
Screenshot 2020-02-08 at 7.32.13 AM.png

step 4: the formula field to concatenate the IDs
you may be able to do this directly in the assignment element - did not test that
Screenshot 2020-02-08 at 7.32.50 AM.png

step 5: the matching rule
Screenshot 2020-02-08 at 7.34.56 AM.png

thats about it - just activate and watch the magic happen!

the main downside to this proof of concept is that the field that is displayed in the alert is the compound key field…which is less than ideal. so instead of using compound key, you could match on one lookup using the dupe rule, and use this flow approach to set a single ID to match against. then you only have one ugly ID show. You can’t do any cross object stuff in the flow, so you only have access to the related records IDs, so options are somewhat limited - but maybe you can figure something out.

fixedInLex2.png

UPDATE: JUNE 2022:
h/t to Cassie Supilowski for sharing that in LEX the duplicate error message comes through beautifully - her screenshots are copied below.

Dupe Error: #

dupeerror.png

View Duplicates Page #

viewdupes.png

 
60
Kudos
 
60
Kudos

Now read this

Dynamic Approvals in Salesforce Using Visual Workflow

We needed a dynamic approval process for our new HR system (FinancialForce HCM), where the 3 approvers for a consultant job requisition needed to be based on the budget managers for the related department, cost center, and region. Budget... Continue →