You need to sign in to do that
Don't have an account?
jsacpt24
Lead dedupe
I wrote a dedupe trigger that is supposed to check to see if the lead already exists in the system by email and contact. If the contact already exist then I want it to add the original lead to a lookup field. If it is not a duplicate then it would mark is_primary__c. I don't see it updating when I go and create a new lead manually. Is there something in my code that is causing it to not work?
trigger findLeadDupes on Lead (before insert) { for (Lead myLead : Trigger.new) { if (myLead.Email != null) { List<Lead> dupes = [SELECT Id FROM Lead WHERE Email = :myLead.Email AND Company = :myLead.Company]; if (dupes.size() > 0) { myLead.Primary_Lead__c = dupes[0].Id; } else { myLead.Primary_Lead__c = null; myLead.Is_Primary__c = True; } } } }
In the above code, you are using before Insert. For before Insert the values that you are entering are not committed to database so it will show you null value.
try the below script
Trigger:
-----------------
trigger findLeadDupes on Lead (after insert) {
if(Trigger.isAfter) {
if(Trigger.isInsert) {
findLeadDupesHandler.onAfterInsert(Trigger.New);
}
}
}
Handler:
-----------------------
public class findLeadDupesHandler {
// Method to execute on After Insert of lead
public static void onAfterInsert(List<Lead> leads) {
Map<String, Id> leadMap = new Map<String, Id>();
List<Lead> updateList = new List<Lead>();
// Lead to get all the lead records based on created Date
List<Lead> leadList = [SELECT Id, Email, Company FROM Lead Order By CreatedDate DESC];
// Loop to iterate over the List of Lead result records
for(Lead lead : leadList) {
string concatinatedValue = lead.Email + ' : ' + lead.Company;
leadMap.put(concatinatedValue, Lead.Id);
}
// Loop to iterate over the list of records
for(Lead lead : leads) {
// Condition to check for the concated string
if(leadMap.containsKey(lead.Email + ' : ' +lead.Company)) {
Lead updateLead = new Lead(Id = lead.Id ,Primary_Lead__c = leadMap.get(lead.Email + ' : ' +lead.Company) );
updateList.add(updateLead);
} else {
Lead updateLead = new Lead(Id = lead.Id ,Is_Primary__c = true);
updateList.add(updateLead);
}
}
try{
update updateList;
} catch(DMLException e) {
System.debug('Unable to update the Lead Record : ' + e.getMessage());
}
}
}
All Answers
In the above code, you are using before Insert. For before Insert the values that you are entering are not committed to database so it will show you null value.
try the below script
Trigger:
-----------------
trigger findLeadDupes on Lead (after insert) {
if(Trigger.isAfter) {
if(Trigger.isInsert) {
findLeadDupesHandler.onAfterInsert(Trigger.New);
}
}
}
Handler:
-----------------------
public class findLeadDupesHandler {
// Method to execute on After Insert of lead
public static void onAfterInsert(List<Lead> leads) {
Map<String, Id> leadMap = new Map<String, Id>();
List<Lead> updateList = new List<Lead>();
// Lead to get all the lead records based on created Date
List<Lead> leadList = [SELECT Id, Email, Company FROM Lead Order By CreatedDate DESC];
// Loop to iterate over the List of Lead result records
for(Lead lead : leadList) {
string concatinatedValue = lead.Email + ' : ' + lead.Company;
leadMap.put(concatinatedValue, Lead.Id);
}
// Loop to iterate over the list of records
for(Lead lead : leads) {
// Condition to check for the concated string
if(leadMap.containsKey(lead.Email + ' : ' +lead.Company)) {
Lead updateLead = new Lead(Id = lead.Id ,Primary_Lead__c = leadMap.get(lead.Email + ' : ' +lead.Company) );
updateList.add(updateLead);
} else {
Lead updateLead = new Lead(Id = lead.Id ,Is_Primary__c = true);
updateList.add(updateLead);
}
}
try{
update updateList;
} catch(DMLException e) {
System.debug('Unable to update the Lead Record : ' + e.getMessage());
}
}
}
it looks like it is saving properly but I am not seeing the duplicates be added to the field or have the is primary boolean checked. Is there something you recommend I do to tweak or check for this?
After saving the code you can go and clone the lead record. In that way, you can validate that the code is working or not. Create a new lead with new email and company name the primary flag should be populated.