Salesforce Duplicate Management can be achieved using a Set that can store the values of that specific field from all existing records and compare it with the list of new records that are going to be inserted. The same approach won’t work if we need to prevent duplicate records based multiple fields, even if we use multiple sets. To solve the issue in Salesforce, one of the solutions is to create a Wrapper class with the hashcode and equals method in the class. The hashcode method creates a unique id for the individual wrapper class instance. This unique id is in turn used by Salesforce to ensure uniqueness of the records. Example: Let us look at two scenarios—one for single field and another one for multiple fields. The user should not be able to create a new lead with the same LASTNAME and EMAIL. SCENARIO 1: Let us consider the method used for single field using the SET. Create a new trigger in lead object and past the below (https://www.freeoceanoffgames.com/) code (https://www.bestapkever.com/). trigger AvoidDuplicate on Lead (before insert) { set<string> newNameSet = new set<string>();newNameSet = new set<string>(); set<string> newEmailSet = new set<string>(); set<string> dbNameSet = new set<string>(); set<string> dbEmailSet = new set<string>(); for(lead newLead : trigger.new){ newNameSet.add(newLead.LastName); newEmailSet.add(newLead.Email); } for(Lead dbLead : [select id, LastName, email from lead where email IN: newEmailSet OR LastName IN: newNameSet]){newNameSet]){ dbNameSet.add(dbLead.LastName); dbEmailSet.add(dbLead.Email); } for(lead newLead : trigger.new){ if(dbNameSet.contains(newLead.LastName) && dbEmailSet.Contains(newLead.Email)) newLead.addError('You are inserting Duplicate lead'); }
You can use Matching Rules that will identify 'what field' and 'how' to match. For example, 'Email Field, Exact Match' or 'Account Name, Fuzzy Match' They don't do anything on their own
Try the Salesforce Duplicate Rule and Matching Rule out of the box functionalities.
https://absyz.com/managing-duplicate-records-in-salesforce/
https://www.veonconsulting.com/matching-duplicate-in-salesforce/
https://www.mstsolutions.com/technical/duplicate-management-in-salesforce-com/
Thanks,
Maharajan.C
Salesforce Duplicate Management can be achieved using a Set that can store the values of that specific field from all existing records and compare it with the list of new records that are going to be inserted.
The same approach won’t work if we need to prevent duplicate records based multiple fields, even if we use multiple sets. To solve the issue in Salesforce, one of the solutions is to create a Wrapper class with the hashcode and equals method in the class. The hashcode method creates a unique id for the individual wrapper class instance. This unique id is in turn used by Salesforce to ensure uniqueness of the records.
Example: Let us look at two scenarios—one for single field and another one for multiple fields.
The user should not be able to create a new lead with the same LASTNAME and EMAIL.
SCENARIO 1:
Let us consider the method used for single field using the SET.
Create a new trigger in lead object and past the below (https://www.freeoceanoffgames.com/) code (https://www.bestapkever.com/).
trigger AvoidDuplicate on Lead (before insert) { set<string> newNameSet = new set<string>();newNameSet = new set<string>(); set<string> newEmailSet = new set<string>(); set<string> dbNameSet = new set<string>(); set<string> dbEmailSet = new set<string>(); for(lead newLead : trigger.new){ newNameSet.add(newLead.LastName); newEmailSet.add(newLead.Email); } for(Lead dbLead : [select id, LastName, email from lead where email IN: newEmailSet OR LastName IN: newNameSet]){newNameSet]){ dbNameSet.add(dbLead.LastName); dbEmailSet.add(dbLead.Email); } for(lead newLead : trigger.new){ if(dbNameSet.contains(newLead.LastName) && dbEmailSet.Contains(newLead.Email)) newLead.addError('You are inserting Duplicate lead'); }
Hi ANANDA,
You can use Matching Rules that will identify 'what field' and 'how' to match. For example, 'Email Field, Exact Match' or 'Account Name, Fuzzy Match' They don't do anything on their own
https://absyz.com/managing-duplicate-records-in-salesforce/
Duplicate Rules will use those Matching Rules to control 'when' and 'where' to find duplicates