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 

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);
    }
}