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
jishan royjishan roy 

when i create new lead and last name is ? as question mark then lastname auto populate with 001, 002.

hii @all

my que is when i create lead record like lastname ? as question mark then record is saved on 001 as last name same as created 2nd lead record and last name ? as questionmark then after saved record last name is 002 so ..... 
but this is worked well when i create single single record but when i insert bulk record 10 record using data loader then all record lastname is 001,001,001 like this i want for 1st record 001,for 2nd record 002,for 3rd record 003 like this please help me out of this  
i Wrote code :

this is my code:
helper:
trigger LeadTriggerLastName on Lead (before insert, before update) {
    if(trigger.isbefore && trigger.isinsert ){
        system.debug('isinsert fired AutoNumberLead');
        AutoNumberLeadhandler.countNo(trigger.new, null);
    }
    else if(trigger.isbefore && trigger.isupdate){
        system.debug('isupdate fired AutoNumberLead');
        AutoNumberLeadhandler.countNoUpdate(trigger.new, trigger.oldmap);
    }
}


handler:
public class AutoNumberLeadhandler {
public static void countNo(List<Lead> newLead, Map<Id,lead> OldLead){
        for (Lead l : newLead) {
            if (l.LastName == '?' && OldLead == null) {
                system.debug('isinsert fired ');
                // Generate the auto-numbered last name
                String autoLastName = 'Lead' + String.valueOf([SELECT count() FROM Lead WHERE LastName LIKE '%Lead%']).leftPad(3, '0');
                l.LastName = autoLastName;
            }
        }
    }
    
    public static void countNoUpdate(List<Lead> newLead, Map<Id,lead> OldLead){
        for(lead le : newLead){
            if(le.lastname == '?' && OldLead.get(le.Id).lastname != le.lastName){
                system.debug('isupdate fired ' );
                String autoLastName = 'Lead' + String.valueOf([SELECT count() FROM Lead WHERE LastName LIKE '%Lead%']).leftPad(3, '0');
                le.LastName = autoLastName;
            }
        }
    }
}



Thank you!