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
Kieran HarrisonKieran Harrison 

Apex Trigger - Duplicate Scan

Hi Guys, 

Just wondering if it is possible to create a trigger whereby everytime a Lead is converted and the user is creating a NEW Account and NEW Opportunity (not converting to an existing Account) that the trigger scans all existing Accounts (org-wide) before the records are created and looks for possible matches on the Date of Birth, Name, and Email fields used on the Lead. 

Essentially the spec is; 

  • User is trying to convert a Lead 
  • User is selecting to create a new Account and Opportunity
  • Trigger scans all existing Accounts for matches on the Date of Birth, Name and Email fields
  • Trigger returns a warning when matches are found

Field API names - 

 

  • Name 
  • Date_of_Birth__pc
  • PersonEmail
Best Answer chosen by Kieran Harrison
Rajesh_ShahRajesh_Shah
I don't know if the stage update fires before the account is created. You will probably have to test it out. 

Regarding formula field, I assume the formula field itself is based on the data from the lead fields. Can't you use those fields in the matching rules? If that is not possible, you can create a non-formula field that is updated by a formula on any update on lead using the same logic as formula field. Then use that field in matching rule. 

All Answers

Rajesh_ShahRajesh_Shah
Hi Kieran,

You can use the Salesforce Duplicate Rule management to identify duplicates. You can use the custom matching critieria that you want in the rules. Once enabled, it will give a warning to the user when he is converting a lead that there is an existing account with matching details. 

Regarding trigger, it would be difficult to handle this. Below is the flow that happens on lead conversion:
  1. New Account, New Contact and (optional) new opportunity is created.
  2. Lead field IsConverted is updated from False to True. 
So if you use the IsConverted field to check, the account would already have been created. 
Kieran HarrisonKieran Harrison
Hi Rajesh, 

Ahhh I see, could we not use the previous stage then in that case?

The issue with using the Duplicate Rule and Matching Rule functions is that you cannot create a matching rule that relies on a formula field. This is an issue for me in my org. 

Could I not just use (Before Insert) for this tirgger?

Thanks,
 
Rajesh_ShahRajesh_Shah
I don't know if the stage update fires before the account is created. You will probably have to test it out. 

Regarding formula field, I assume the formula field itself is based on the data from the lead fields. Can't you use those fields in the matching rules? If that is not possible, you can create a non-formula field that is updated by a formula on any update on lead using the same logic as formula field. Then use that field in matching rule. 
This was selected as the best answer
Kieran HarrisonKieran Harrison
Thanks very much, Rajesh.

A little bit fiddly but I got there in the end.