You need to sign in to do that
Don't have an account?
Kieran 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
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
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:
- New Account, New Contact and (optional) new opportunity is created.
- 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.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,
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.
A little bit fiddly but I got there in the end.