top of page

Salesforce Duplicate Matching in Apex

Updated: Jul 27, 2023


Salesforce provides an excellent way to manage duplicate records for Account, Contact, Lead and Individual objects. Before creating a new record, you can set up rules that will alert you if the record you’re about to add is possibly a duplicate.

Managing duplicate records consists of 2 parts:

Duplicate Rules

A duplicate rule allows you to determine what happens when a possible duplicate record is about to be created. These duplicate rules can be created in Setup in Salesforce. There are many options you can specify as to what happens, such as not allowing a record to be created if a duplicate exists, but we’ll concentrate on creating a duplicate rule which we can then make use of in Apex.

In a duplicate rule, you can use the standard matching rules which Salesforce provides or you can create your own.

Matching Rules

A matching rule defines what exactly a duplicate is and how they are identified. Take a look at this example:

In this matching rule, I specify that the account name must be a fuzzy match. By using fuzzy matching, we won’t need an exact match as many companies can have different abbreviations of their name, e.g. “Pfizer Inc” and “Pfizer inc.” and “Pfizer” will all be matched as duplicates. Similarly, the address will also be matched fuzzy, so for street, 123 Market Street, Suite 100, and 123 Market Drive, Suite 300 will both be a match. As for the country, this will be an exact match as this field is a dropdown list.

Duplicate Handling in Apex

Once you have defined your matching and duplicate rules, you can make use of these in Apex. Say you wanted to create a new account record via Apex, you would first want to check for duplicates the same way a user would via the UI.

To accomplish this, you would need to carry out the following in Apex:

  1. Create the new record (but not insert).

  2. Use the FindDuplicates class to find the duplicates.

  3. Get the duplicate results.

  4. Get the matching results.

Account account = new Account();
account.Name = 'Microsoft';
account.BillingStreet = '15010 NE 36th Street';
account.BillingCity = 'Washington';
account.BillingPostalCode = '98052';
account.BillingCountry = 'United State';
List acctList = new List();
Datacloud.FindDuplicatesResult[] accountDuplicates = Datacloud.FindDuplicates.findDuplicates(acctList);
for (Datacloud.FindDuplicatesResult dupeResult: accountDuplicates) {
  for (Datacloud.DuplicateResult dupeRes: dupeResult.getDuplicateResults()) {
    for (Datacloud.MatchResult matchRes: dupeRes.getMatchResults()) {
      for (Datacloud.MatchRecord matchRec: matchRes.getMatchRecords()) {
        Account matchingAccount = (Account) matchRec.getRecord();
        return matchingAccount;

Using this code, you can ensure you don’t create duplicate records without having to manually find the duplicate records, or specifying rules any different than users would use through the UI.

Happy coding!

39 views0 comments


I'm a lead software developer currently working at AG Grid in London.

Technologies I'm currently focused on include Salesforce, .NET Core, Angular, SQL, React and Azure.

Other than that, in my spare time I watch the Arsenal at the Emirates, work on side projects or watch sports. Oh, and I'm also a part-time body builder.

You can contact me at


About Viqas Hussain

bottom of page