This is my first question here. I'm working with a company that allows duplicate leads, but they want all the duplicate leads to have the same owner. I'm trying to write a before insert, before update trigger, but I can't make it work. Some help would be awesome!!
Thank you so much!!!
Here's a sample of my trigger.
trigger DuplicateLeadOwner on Lead (before insert, before update) {
String leadEmail;
String leadId;
List <Lead> dupeleads = [SELECT Id, OwnerId FROM Lead WHERE Email = :leadEmail AND Id != :leadId];
for(Lead l : Trigger.new){
//Make sure the lead has an email address
if (l.Email != null){
//Find a duplicate
String leadEmail = l.Email;
String leadId = l.Id;
//If a duplicate is found, update the Owner field.
if(dupeleads.size() > 0){
dupeleads[0].OwnerId = l.OwnerId;
dupeleads[0].Email = l.Email;
}
}
}
}