You need to sign in to do that
Don't have an account?
Rahul
hi friends, this code is working fine but Iam not able to change the owner of the new lead manually if email address is not null. do anyone have Idea?
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 = [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 = [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);
}
}
The insert process differs slightly from the update process. I have added a check to prevent existing records from being able to update themselvesnby eliminating them from the records returned from Salesforce. Hope this works.
All Answers
Do you know how to solve this? because whenever iam creating a lead with a not matching email address and then changing the owner of the lead, its not changed. The owner remains the same.
Example: I have create a lead with email address "test@gmail.com" and have set owner as Admin, Tomorrow you create another duplicate lead record with the same email address i.e "test@gmail.com" and now you save the record, the Owner should automatically change to the first created record owner i.e Admin. So the Above code is working fine, the functionality which I have mentioned is working. The only problem is when I create a new Lead with and Unmatching email address(The Email address not present in any lead Record) and save it and change the owner manually, the owner is not getting changed. The owner remains the same owner.
The insert process differs slightly from the update process. I have added a check to prevent existing records from being able to update themselvesnby eliminating them from the records returned from Salesforce. Hope this works.