function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RahulRahul 

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);
    }
    
   
}
Best Answer chosen by Rahul
Steven NsubugaSteven Nsubuga
I think the solution is to prevent a Lead from updating itself. I made some minor tweaks in the code. 
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);
    }
}


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

Steven NsubugaSteven Nsubuga
The problem is that whenever you attempt to change the owner of a lead that has an email address set, your trigger queries for all leads with that email address and sets the owner of the edited lead to one of the owners of leads with the same email address as the one you are editing. In most cases the trigger will set the owner back to the original owner, especially if the lead's email address only appears once, that is on the record being edited.
RahulRahul
"In most cases the trigger will set the owner back to the original owner, especially if the lead's email address only appears once, that is on the record being edited"

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.
Steven NsubugaSteven Nsubuga
What exactly are you trying to achieve? Can you outline the steps you are taking, or maybe use pseudo code to state your proposed solution?
RahulRahul
I want that the owner of the duplicate lead record should be updated as the First record owner. Duplicate is identified with the Email address.
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. 
Steven NsubugaSteven Nsubuga
I think the solution is to prevent a Lead from updating itself. I made some minor tweaks in the code. 
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);
    }
}


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.
This was selected as the best answer
RahulRahul
Thanks steven. You helped me a Lot :) 
Steven NsubugaSteven Nsubuga
My pleasure Sumit!