You need to sign in to do that
Don't have an account?
Rahul
Hello friends, I need this trigger should fire only once whenever a new lead is inserted. if we want to change the owner after lead insertion we should be able to change. Need help
trigger AssignToFirstOwnerForDuplicteLeads on Lead (before insert, before update) {
set <string> newEmaiSet = new set<string>();
set<string> newPhNumSet = new set<string>();
for(lead l1: trigger.new) {
if(l1.Email != null) {
newEmaiSet.add(l1.Email);
}
if(l1.phone != null) {
newPhNumSet.add(l1.phone);
}
}
List<lead> leadWithMatchingEmail = new List<lead>();
if (Trigger.isUpdate) {
leadWithMatchingEmail = [select id, email, OwnerId from lead where email in :newEmaiSet and Id NOT IN:trigger.newMap.keyset()];
}
if (Trigger.isInsert) {
leadWithMatchingEmail = [select id, email, OwnerId from lead where email in :newEmaiSet];
}
// List<lead> leadWithMatchingPhone = [select id, phone, OwnerId from lead where email in :newPhNumSet];
Map<string, id> emailVSId = new Map<string, id>();
Map<string, id> phoneVSId = new Map<string, id>();
for(lead l1: leadWithMatchingEmail) {
emailVSId.put(l1.Email, l1.OwnerId);
}
for(lead l1: trigger.new) {
if(emailVSId.containsKey(l1.Email)) {
l1.OwnerId = emailVSId.get((l1.Email));
}
// if(phoneVSId.containsKey(l1.phone)) {
// l1.OwnerId = phoneVSId.get((l1.phone));
// }
system.debug('l1::@@'+l1);
}
}
set <string> newEmaiSet = new set<string>();
set<string> newPhNumSet = new set<string>();
for(lead l1: trigger.new) {
if(l1.Email != null) {
newEmaiSet.add(l1.Email);
}
if(l1.phone != null) {
newPhNumSet.add(l1.phone);
}
}
List<lead> leadWithMatchingEmail = new List<lead>();
if (Trigger.isUpdate) {
leadWithMatchingEmail = [select id, email, OwnerId from lead where email in :newEmaiSet and Id NOT IN:trigger.newMap.keyset()];
}
if (Trigger.isInsert) {
leadWithMatchingEmail = [select id, email, OwnerId from lead where email in :newEmaiSet];
}
// List<lead> leadWithMatchingPhone = [select id, phone, OwnerId from lead where email in :newPhNumSet];
Map<string, id> emailVSId = new Map<string, id>();
Map<string, id> phoneVSId = new Map<string, id>();
for(lead l1: leadWithMatchingEmail) {
emailVSId.put(l1.Email, l1.OwnerId);
}
for(lead l1: trigger.new) {
if(emailVSId.containsKey(l1.Email)) {
l1.OwnerId = emailVSId.get((l1.Email));
}
// if(phoneVSId.containsKey(l1.phone)) {
// l1.OwnerId = phoneVSId.get((l1.phone));
// }
system.debug('l1::@@'+l1);
}
}