function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ScottB3ScottB3 

Use Matching Rules in Apex to find all leads which match an account

Salesforce has a Matched Leads Component that can be placed on an account to show which leads match the account.

The problem is that the layout is not conducive to the work that my company needs to do and I need to create a custom component which uses the same matching logic but displays in a more traditional table format.

I cannot find any documentation on where to start on how to utilize the matching rules engine in Apex code to get a list of leads that I can then manipulate into our needed format.

User-added image
ShirishaShirisha (Salesforce Developers) 
Hi Scott,

Greetings!

There is a pre-defined class which can help you to get the records by matching rule.Please find the below thread for more information.

https://salesforce.stackexchange.com/questions/163619/accessing-a-duplicate-rules-matching-rules-via-apex

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
 
ScottB3ScottB3
Thanks Shirisha,
Still struggling with how to handle these in Apex.
I see the classes exist, but what's the order of operations to go from having an account ID to actually getting a list of leads?
ScottB3ScottB3
Or even if you could provide an idea of a first step on the path, that would be helpful.
Matthew ZMatthew Z

Little late to the party, but I was looking into this and I found this article: 

https://idealistconsulting.com/blog/6-tips-duplicate-management-apex

What you could do is instantiate a new List of Leads in Apex using the Account data. Fill in the fields that will be used in the matching rule (i.e, lead.company = account.name, lead.phone = account.phone, etc..)

Then use: 

List<Datacloud.FindDuplicatesResult> results = Datacloud.FindDuplicates.findDuplicates(List<Lead>) which seems to use existing matching rules to return duplicates (if found), You can then parse your lead list, compare against the results and determine for each lead
if matches were found & how many and which records. 

I havent tested it out but I plan on implementing it soon for a project. 

Hope that helps. 

Matthew ZMatthew Z
(you can also manipulate and display those leads in your component)