I created a matching rule and duplicate rule regarding lead company name and i tested by creating records with company name as test and test data The record is saving without showing any duplicates found. For reference please find below screenshots .Is there any other way to achieve this.
Trigger DuplicateLeadPreventer on Lead
(before insert, before update) {
//Since only certain leads will match, put them in a separate list
List<Lead> LeadstoProcess = new List<Lead> ();
//Company to Lead Map
Map<String, Lead> leadMap = new Map<String, Lead>();
for (Lead lead : Trigger.new) {
//Only process for Leads in our RecordTypeMap
// Make sure we don't treat an Company name that
// isn't changing during an update as a duplicate.
if (
(lead.company != null) &&
(Trigger.isInsert ||
(lead.company != Trigger.oldMap.get(lead.Id).company))
)
{
// Make sure another new lead isn't also a duplicate
if (leadMap.containsKey(lead.company)) {
lead.company.addError('Another new lead has the '
+ 'same company name.');
} else {
leadMap.put(lead.company , lead);
LeadstoProcess.add(lead);
}
} //end RT If Check
} //End Loop
/*
Using a single database query, find all the leads in
the database that have the same company address as any
of the leads being inserted or updated.
*/
Set<String> ExistingCompanies = new Set<String> ();
for (Lead l: [Select Id, Company from Lead WHERE Company IN :leadMap.keyset()]) {
ExistingCompanies.add(l.Company);
}
//Now loop through leads to process, since we should only loop if matches
/* for (Lead l : LeadstoProcess) {
if (ExistingCompanies.contains(l.company) ) {
l.company.addError('A lead with this company '
+ 'name already exists.');
} */
}