What are Duplicate Rules and Matching Rules? How to Invoke Duplicate and Matching Rules in Salesforce without performing DML operations?

 Definition:

1.       Duplicate rules are configurations that define the conditions under which a record is considered a duplicate. These rules are applied to standard and custom objects in Salesforce.

2.       Matching rules determine how Salesforce identifies duplicate records when new records are created or existing records are updated. They define which fields to compare and the criteria for matching.

3.       Duplicate rules use matching criteria to determine if a new or updated record matches existing records in the Salesforce organization. Matching criteria can include exact matching or more flexible fuzzy matching based on specific fields.

Please note that we can only invoke these while performing DML operations like Insert or Update.

Duplicate Rule:

It’s Corresponding Matching Rule:

The Standard Contact Matching Rule criteria:

 

Blocking or Alerting:

Duplicate rules can be configured to either block the creation or update of a record if a potential duplicate is detected or to allow the operation but generate an alert for users.


Order of Execution:

Duplicate rules are evaluated in the order specified by the Duplicate Rule Processing Order. Salesforce processes the rules one by one, starting from the top of the list.

If a record matches the criteria specified in a duplicate rule, the corresponding actions are taken (e.g., blocking the record creation, and generating an alert).

Once a duplicate rule is triggered and its actions are executed, the subsequent rules in the list are not processed.

Let’s see how it works out of the Box first.

Let’s say we already have a contact record with FirstName = Bharathwaj and LastName = Sridhar

When we are trying to create another Contact record with the same details we get an alert popover in the UI saying duplicate records found. Click on View Duplicates will allow the user to see Duplicate records in the UI. As we have enabled Allow create it still allows us to create the record.

                                                      But these popovers are only triggered on Record Pages. 

How can we trigger these duplicate rules in a Custom LWC component?

Step1. Collect necessary data from the LWC component and make a server call using wire or ConnectedCallback() and pass the necessary data to the Apex Controller.

Step2. Our Apex controller collects the data and invokes the Duplicate Rules for the Contact object as in this case we passing a list of contact records in the Datacloud.FindDuplicates.findDuplicates(conList); where conList is the list of contacts that are sent to the findDuplicates() method.




Step3. If you follow the above code, we can trigger the firing of Respective duplicate Rules based on the SObject type we pass in the findDuplicates() method. We can return the Duplicate records to client side and display potential Duplicates without performing any DML operations!

Happy Coding!






Comments

Popular posts from this blog

Apex Best Practices

LWC Best Practices