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 question mark then lastname auto populate with lead 001, lead002.

hello,

my requirement is when lead lastname is ? questionmark the display lastname is lead 001, lead002 like this so i write trigger but it will work only single record crerated then it will run perfect when i insert record in bulk like data loader then all record lastname had showed lead001, lead001 like that so i want lead001, lead002.

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;
            }
        }
    }
}
john alieeejohn alieee
It's not clear which system or software you are using to create new leads, so I can't provide specific instructions on how to fix this issue. However, I can offer some general advice that may help you troubleshoot the problem.
First, it's possible that the system or software you're using has a default setting that auto-populates the last name field with a generic value (like "lead001" or "lead002") when the last name is left blank or contains a special character like a question mark. In this case, you may be able to change the default setting or create a new rule that specifies how the system should behave when the last name is left blank or contains a special character.
Second, it's also possible that the issue is caused by a bug or glitch in the system or software. If this is the case, you may need to contact the technical support team for the system or software and report the issue. They may be able to help you troubleshoot the problem or provide a software update that resolves the issue.
In either case, it's important to identify the root cause of the problem and take steps to resolve it to ensure that your lead data is accurate and complete. 
CC: 
extraappliance.ca
missmillycleaning.com
jishan royjishan roy
hi john,

so basically i used salesforce and my requirement is when i create lead record and i put last name is '?' (questionmark) then that lead record is automatic saved the last name like (lead001) like that


soo its working on manually process but when i insert bulk record on that lastname is '?' then all records  lastname displayed (lead001) like this but i want -- 1st record lastname --lead001, 2nd record lastname lead002 etc..

Thanks in advanced.