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
Gary Payne 2Gary Payne 2 

How to pre-populate the Lead's Last Name field when creating a new Lead record?


Many times a CSR does not acquire the prospect's Last Name when creating a new Lead record. For this reason I need a way to pre-populate the Lead's Last Name field with a value like "Customer" so the new record can be saved, as Last Name is a required field. The CSR can always overwrite the Last Name field value either before or after saving the initial record. I tried to create a workflow that adds the text "Customer" to the Lead's Last Name field if the value in the Last Name field was just the letter "a", but it is firing and overwriting any value entered into the Last Name field upon saving the record. The workflow was set to fire only when a new record was created. Any suggestions would be appreciated.
Best Answer chosen by Gary Payne 2
Gary Payne 2Gary Payne 2
I was able to resolve this problem by creating a workflow rule that uses the following formula in the update action:
if(LastName = "a", "Customer", LastName).  This formula allows the user to save the new Lead record with the value of "a" in the Last Name field and then updates the Last Name field with "Customer".  It only fires on new Lead creation.  If a regular Last Name is entered, not just "a", in the new Lead record then the Last Name field is not updated.

All Answers

SwethaSwetha (Salesforce Developers) 
HI Gary,
The workflow approach is right for your scenario. Make sure you have set the workflow evaluation criteria is set to when record is "created" .

You can set rule criteria that when lastName of lead is null,the workflow should be fired.

This will ensure the LastName field is populated when record is created with empty lastName.

Related: https://help.salesforce.com/articleView?id=workflow_rules_define.htm&type=5
https://salesforce.stackexchange.com/questions/288662/auto-populate-the-company-field-during-lead-creation

​​​​​​​Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Gary Payne 2Gary Payne 2
Hi Swetha,
Thank you for the quick reply.  Unfortunately the new Lead record cannot be saved if the Last Name field is null, as it is a required field. Your workflow solution will work for the other fields that are not required fields because a new Lead record can be saved with null valued non-required fields.  My challenge is how to populate a required Last Name field on a new Lead record with a text string , like "Customer", before the record is saved for the first time.  I'm wondering if this is possible with a Lead Trigger, using the Before Insert action.
ravi soniravi soni
Hi Gary, 
I think your requirment is that whenever the user create a new lead then lastName must be populated with 'Customer'.
so you can write a trigger on before insert action on lead object.
try this following trigger.
trigger PopulateLastNameOfLead on Lead (before insert) {
    if(trigger.IsBefore && trigger.IsInsert){
        for(lead l : trigger.new){
            l.LastName = 'Customer';
        }
        
    }

}
let me know if it's works and mark the best so that it can help to others.
Thank You

 
Gary Payne 2Gary Payne 2
Thanks Veer Soni, The trigger still produces the "The required fields must be completed: Last Name" error when attempting to save the new Lead record when the Last Name field has been populated. The trigger is not pre-populating the Last Name field.
Andrew GAndrew G
Issue is the Execution of Save Order.  System validations occur before Before Triggers.

Last Name in Leads is a compulsory Salesforce field and in the Save Order that will execute before any Trigger or Flow automation.

Next issue is that it is one of those standard fields that doesn't allow a default value so you can't by-pass it that way.

So, if you really want this behaviour (i.e. that the person creating the lead doesn't need to enter a last name) you will have to create a customisation of some description.  For example, a VF page which displays the fields and passes to a controller to create the Lead in the back end.  Or you could create your own Lead object, but note you will lose alot of the standard Lead functions if you do this.  

Personally, I like saying NO to my customer and I would say that its in the too hard basket for possible return of investment.  And then get the customer to really think about what they are trying to do with Leads.

regards
Andrew
 
ravi soniravi soni
Hi Gary,
Please fill up the last name fill with any string value like 'xyz' before save Lead record and after save lead the last Name will be populate with customer. If any required field is not filled up in record then trigger is not excuted.
so fill up the lastName with any string value and after insert the lead record's lastName will be customer.

First You should Fill up Required LastName and see the Lead's Record LastName With Customer In after Insert Case. I have tried this requirment on my org and where it is working good.

Let Me know this time your issue is solved or not. 
Thank You 
Gary Payne 2Gary Payne 2
If a value, like "a" or "xyz", is entered into the Lead's Last Name field the record will save and the Last Name field will be updated to "Customer". The problem is that the trigger is overwriting any value in the Last Name field to "Customer, so if the user has entered a valid Last Name, like "Smith", it is being overwritten with "Customer". What needs to be added to the trigger to prevent overwriting a valid text string, but also overwrite the value if it is just "a" or "xyz"?
Gary Payne 2Gary Payne 2
I was able to resolve this problem by creating a workflow rule that uses the following formula in the update action:
if(LastName = "a", "Customer", LastName).  This formula allows the user to save the new Lead record with the value of "a" in the Last Name field and then updates the Last Name field with "Customer".  It only fires on new Lead creation.  If a regular Last Name is entered, not just "a", in the new Lead record then the Last Name field is not updated.
This was selected as the best answer