You need to sign in to do that
Don't have an account?
Dnyaneshwar
Handle lead duplication in after trigger
Hi All,
I am trying to check if Lead to insert is duplicate and if it is then store it in separate custom object(Duplicate_Lead__c). but in query it is returning the record i am inserting i.e. for every lead i insert this trigger is matching its fields with itself and creating duplicate record in custom object.
How to avoid this? Also i want to update a checkbox on duplicate Lead record, is it possible in same after trigger?
trigger LeadDuplicateTrigger on Lead(after insert) {
List Exleads = [Select id, name, email, MobilePhone, Program__c From Lead];
List DupLeads = new List ();
for(Lead l : Trigger.new){
for(Lead Ele : Exleads){
if(( (l.email == Ele.email) && (l.Program__c==Ele.Program__c) ) || ( (l.MobilePhone == ELe.MobilePhone) && (l.Program__c==Ele.Program__c) )){
Duplicate_Lead__c DPLead = new Duplicate_Lead__c();
DPLead.Name = ‘Duplicate Of’+’ ‘+ Ele.Name;
DPLead.Admission_Stage__c = l.Admission_Stage__c;
DPLead.Admission_Status__c = l.Admission_Status__c;
DPLead.Agency__c = l.Agency__c;
DPLead.Initial_Owner__c = l.Initial_Owner__c;
DPLead.Initial_program__c = l.Initial_program__c;
DPLead.Program__c = l.Program__c;
DPLead.Lead_State__c = l.Lead_State__c;
DPLead.Test_Score__c = l.Test_Score__c;
DupLeads.add(DPLead);
}
}
}
Insert DupLeads;
I am trying to check if Lead to insert is duplicate and if it is then store it in separate custom object(Duplicate_Lead__c). but in query it is returning the record i am inserting i.e. for every lead i insert this trigger is matching its fields with itself and creating duplicate record in custom object.
How to avoid this? Also i want to update a checkbox on duplicate Lead record, is it possible in same after trigger?
trigger LeadDuplicateTrigger on Lead(after insert) {
List Exleads = [Select id, name, email, MobilePhone, Program__c From Lead];
List DupLeads = new List ();
for(Lead l : Trigger.new){
for(Lead Ele : Exleads){
if(( (l.email == Ele.email) && (l.Program__c==Ele.Program__c) ) || ( (l.MobilePhone == ELe.MobilePhone) && (l.Program__c==Ele.Program__c) )){
Duplicate_Lead__c DPLead = new Duplicate_Lead__c();
DPLead.Name = ‘Duplicate Of’+’ ‘+ Ele.Name;
DPLead.Admission_Stage__c = l.Admission_Stage__c;
DPLead.Admission_Status__c = l.Admission_Status__c;
DPLead.Agency__c = l.Agency__c;
DPLead.Initial_Owner__c = l.Initial_Owner__c;
DPLead.Initial_program__c = l.Initial_program__c;
DPLead.Program__c = l.Program__c;
DPLead.Lead_State__c = l.Lead_State__c;
DPLead.Test_Score__c = l.Test_Score__c;
DupLeads.add(DPLead);
}
}
}
Insert DupLeads;
List<Lead> Exleads = [Select id, name, email, MobilePhone, Program__c From Lead WHERE Id NOT IN : Trigger.new ];
List <Duplicate_Lead__c> DupLeads = new List <Duplicate_Lead__c>();
List<Lead> dupLeadsDelete = new List<Lead>();
for(Lead l : Trigger.new){
for(Lead Ele : Exleads){
if(( (l.email == Ele.email) && (l.Program__c==Ele.Program__c) ) || ( (l.MobilePhone == ELe.MobilePhone) && (l.Program__c==Ele.Program__c) )){
Duplicate_Lead__c DPLead = new Duplicate_Lead__c();
DPLead.Name = 'Duplicate Of'+' '+ Ele.Name;
DPLead.Admission_Stage__c = l.Admission_Stage__c;
DPLead.Admission_Status__c = l.Admission_Status__c;
DPLead.Agency__c = l.Agency__c;
DPLead.Initial_Owner__c = l.Initial_Owner__c;
DPLead.Initial_program__c = l.Initial_program__c;
DPLead.Program__c = l.Program__c;
DPLead.Lead_State__c = l.Lead_State__c;
DPLead.Test_Score__c = l.Test_Score__c;
Lead updLead=[select id from Lead where id = :Ele.id];
updLead.Duplicacy__c = true;
Update updLead;
Lead DelLead=[select id from Lead where id = :l.id];
Delete DelLead;
DupLeads.add(DPLead);
// Add the Duplicate lead to be deleted to a list
Lead ledDel = new Lead(Id=l.Id);
dupLeadsDelete.add(ledDel);
}
}
}
Insert DupLeads;
if(!dupLeadsDelete.isEmpty) Delete dupLeadsDelete;
for(Lead lead : Trigger.new){
if(trigger.NewMap.get(lead.Id).Duplicacy__c){
lead.addError('The record you attempted to access has been deleted. The user who deleted this record may be able to recover it from the Recycle Bin. Deleted data is stored in the Recycle Bin for 15 days.');
}
All Answers
Modify your query in 2 nd line as given below to avoid fetching record which is beeing inserted.
List Exleads = [Select id, name, email, MobilePhone, Program__c From Lead WHERE Id NOT IN : Trigger.new };
Update Value true in the chaeck box field u want to update
DPLead.Checkboxfield__c = True;// Replace Checkboxfield__c with API name of ur check box field
Let me know if it helps.
Thanks,
Nagendra
Thanks for fast reply.. I have worked out a solution based on your suggestion. This code will update existing duplicate lead(record 1), delete inserting duplicate lead (record 2) and create custom object record with inserted duplicate lead (record 2).
Although when new duplicate record is deleted i want to show custom message instead of
Record deleted
The record you attempted to access has been deleted. The user who deleted this record may be able to recover it from the Recycle Bin. Deleted data is stored in the Recycle Bin for 15 days.
Any suggestion?
trigger LeadDuplicateTrigger on Lead(after insert) {
List<Lead> Exleads = [Select id, name, email, MobilePhone, Program__c From Lead WHERE Id NOT IN : Trigger.new ];
List <Duplicate_Lead__c> DupLeads = new List <Duplicate_Lead__c>();
for(Lead l : Trigger.new){
for(Lead Ele : Exleads){
if(( (l.email == Ele.email) && (l.Program__c==Ele.Program__c) ) || ( (l.MobilePhone == ELe.MobilePhone) && (l.Program__c==Ele.Program__c) )){
Duplicate_Lead__c DPLead = new Duplicate_Lead__c();
DPLead.Name = 'Duplicate Of'+' '+ Ele.Name;
DPLead.Admission_Stage__c = l.Admission_Stage__c;
DPLead.Admission_Status__c = l.Admission_Status__c;
DPLead.Agency__c = l.Agency__c;
DPLead.Initial_Owner__c = l.Initial_Owner__c;
DPLead.Initial_program__c = l.Initial_program__c;
DPLead.Program__c = l.Program__c;
DPLead.Lead_State__c = l.Lead_State__c;
DPLead.Test_Score__c = l.Test_Score__c;
Lead updLead=[select id from Lead where id = :Ele.id];
updLead.Duplicacy__c = true;
Update updLead;
Lead DelLead=[select id from Lead where id = :l.id];
Delete DelLead;
DupLeads.add(DPLead);
}
}
}
Insert DupLeads;
}
trigger LeadDuplicateTrigger on Lead(before insert) {
List<Lead> Exleads = [Select id, name, email, MobilePhone, Program__c From Lead ];// No need to put where clause as record is not inserted yet
List <Duplicate_Lead__c> DupLeads = new List <Duplicate_Lead__c>();
for(Lead l : Trigger.new){
for(Lead Ele : Exleads){
if(( (l.email == Ele.email) && (l.Program__c==Ele.Program__c) ) || ( (l.MobilePhone == ELe.MobilePhone) && (l.Program__c==Ele.Program__c) )){
Duplicate_Lead__c DPLead = new Duplicate_Lead__c();
DPLead.Name = 'Duplicate Of'+' '+ Ele.Name;
DPLead.Admission_Stage__c = l.Admission_Stage__c;
DPLead.Admission_Status__c = l.Admission_Status__c;
DPLead.Agency__c = l.Agency__c;
DPLead.Initial_Owner__c = l.Initial_Owner__c;
DPLead.Initial_program__c = l.Initial_program__c;
DPLead.Program__c = l.Program__c;
DPLead.Lead_State__c = l.Lead_State__c;
DPLead.Test_Score__c = l.Test_Score__c;
// l.Duplicacy__c = true; // This check box should be not be updated as record will ot save. For no records this will be checked since record will be saved in Duplicate_Lead__c objecct if it is Duplicate, not in lead table.
DupLeads.add(DPLead);
}
}
}
Insert DupLeads;
// Stop inserting Duplicate lead and throw error
for(Lead lead : Trigger.new){
if(lead.Duplicacy__c)
lead.addError('Dupliocate Lead ');
return null;
}
}
In my case i want user to add duplicate Lead record (data of which will be moved other object record ) and then delete this record. So user will not see any error.
I just want to customize below message after record is deleted.
Record deleted
The record you attempted to access has been deleted. The user who deleted this record may be able to recover it from the Recycle Bin. Deleted data is stored in the Recycle Bin for 15 days.
List<Lead> Exleads = [Select id, name, email, MobilePhone, Program__c From Lead WHERE Id NOT IN : Trigger.new ];
List <Duplicate_Lead__c> DupLeads = new List <Duplicate_Lead__c>();
List<Lead> dupLeadsDelete = new List<Lead>();
for(Lead l : Trigger.new){
for(Lead Ele : Exleads){
if(( (l.email == Ele.email) && (l.Program__c==Ele.Program__c) ) || ( (l.MobilePhone == ELe.MobilePhone) && (l.Program__c==Ele.Program__c) )){
Duplicate_Lead__c DPLead = new Duplicate_Lead__c();
DPLead.Name = 'Duplicate Of'+' '+ Ele.Name;
DPLead.Admission_Stage__c = l.Admission_Stage__c;
DPLead.Admission_Status__c = l.Admission_Status__c;
DPLead.Agency__c = l.Agency__c;
DPLead.Initial_Owner__c = l.Initial_Owner__c;
DPLead.Initial_program__c = l.Initial_program__c;
DPLead.Program__c = l.Program__c;
DPLead.Lead_State__c = l.Lead_State__c;
DPLead.Test_Score__c = l.Test_Score__c;
Lead updLead=[select id from Lead where id = :Ele.id];
updLead.Duplicacy__c = true;
Update updLead;
Lead DelLead=[select id from Lead where id = :l.id];
Delete DelLead;
DupLeads.add(DPLead);
// Add the Duplicate lead to be deleted to a list
Lead ledDel = new Lead(Id=l.Id);
dupLeadsDelete.add(ledDel);
}
}
}
Insert DupLeads;
if(!dupLeadsDelete.isEmpty) Delete dupLeadsDelete;
for(Lead lead : Trigger.new){
if(trigger.NewMap.get(lead.Id).Duplicacy__c){
lead.addError('The record you attempted to access has been deleted. The user who deleted this record may be able to recover it from the Recycle Bin. Deleted data is stored in the Recycle Bin for 15 days.');
}
finally solved!!!