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
tgk1tgk1 

Simple trigger to pull Lead ID

Hi everyone-

 

I could really use some help with a simple trigger.  When a lead record is saved I need an apex trigger to populate a field called "Lead_Id__c" with the lead record's 18 digit ID.  Can someone please help?  Thanks very much in advance!

 

-Tom

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Ashan, Code is not bulk enabled.

And also it wont work if any string is entered in Lead_Id__c, as you are checking just null condition.

 

check if this works.

 

trigger LeadAfterInsert on Lead (after insert, after update) 
{
	List<Lead> lstLeadstoupdate = new List<Lead>();
	if(trigger.isInsert)
	{
	    for(Integer i = 0; i < Trigger.new.size(); i++)
	    {
	    	String str = string.valueof(Trigger.new[i].Id);
	        lstLeadstoupdate.add(new Lead(Id = Trigger.new[i].Id, Lead_Id__c = str.substring(0, 15)));
	    }
	}
	else
	{
	    for(Integer i = 0; i < Trigger.new.size(); i++)
	        if(Trigger.old[i].Lead_Id__c != null && Trigger.new[i].Lead_Id__c !=  Trigger.old[i].Lead_Id__c)
	        {
	        	String str = string.valueof(Trigger.new[i].Id);
	            lstLeadstoupdate.add(new Lead(Id = Trigger.new[i].Id, Lead_Id__c = str.substring(0, 15)));
	        }
	        else if(Trigger.old[i].Lead_Id__c == null)
	        {
	        	String str = string.valueof(Trigger.new[i].Id);
	            lstLeadstoupdate.add(new Lead(Id = Trigger.new[i].Id, Lead_Id__c = str.substring(0, 15)));
	        }
	
	}
	
	if(!lstLeadstoupdate.isEmpty())
	    update lstLeadstoupdate;
}

 

All Answers

tharristharris

You could do this with an after insert trigger on Lead, ex.:

 

trigger LeadAfterInsert on Lead (after insert) {
    List<Lead> toupdate = [select id, Lead_Id__c from Lead where id in :Trigger.new];

    for(Lead l : toupdate)
        l.Lead_Id__c = l.id;
       
    update toupdate;
}

 

This is assuming your Lead_Id__c field is an 18-character text field.

 

This wouldn't work as a before insert trigger because the records will not have ID's that can be read at that point.

tgk1tgk1

Thanks for the help, I really appreciate it.  The code works fine when a lead record is created, but I'm getting system errors when I try to update a lead record.  I would need to be able to have this work updating leads as well.  I added;

 

trigger LeadAfterInsert on Lead (after insert, after update) {

 

But that's what's throwing the system error.  Do you know how I can get this to work for updating lead records?  Thanks again!

AshanAshan

dont use the update call in an after update trigger without a condition. then it will iterate without stopping.

You can use before update where you dont have to use update call

tgk1tgk1

Thanks Ashan. I took out the update statement at the end but I'm still having trouble getting this to work properly.  Do you have any idea why it's still not working?  Thanks!

Rahul SharmaRahul Sharma

Modified a code a bit, check now, its no event, after update and adter insert.

 

trigger LeadAfterInsert on Lead (after insert, after update) 
{
List<Lead> lstLeadstoupdate = new List<Lead>();
if(trigger.isInsert)
{
    for(Integer i = 0; i < Trigger.new.size(); i++)
    {
        lstLeadstoupdate.add(new Lead(Id = Trigger.new[i].Id, Lead_Id__c = Trigger.new[i].Id));
    }
}
else
{
    for(Integer i = 0; i < Trigger.new.size(); i++)
        if(Trigger.old[i].Lead_Id__c != null && Trigger.new[i].Lead_Id__c !=  Trigger.old[i].Lead_Id__c)
        {
            lstLeadstoupdate.add(new Lead(Id = Trigger.new[i].Id, Lead_Id__c = Trigger.new[i].Id));
        }

}
if(!lstLeadstoupdate.isEmpty())
    update lstLeadstoupdate;
}

 Hope it helps :)

tgk1tgk1

Thanks Rahul!  This works perfect when a new lead record is created, but I just tested it out against existing lead records and the Lead_Id__c field doesn't populate if the record is updated.  Any ideas??  I really appreciate your help!

Rahul SharmaRahul Sharma

tried in my org, work fine.

how are you updating lead records, which fields?

AshanAshan

This will work

 

trigger LeadAfterInsert on Lead (after insert)

{   

List<Lead> toupdate = [select id, Lead_Id__c? from Lead where id in :Trigger.new];


    for(Lead l : toupdate)

{

if(l.Lead_Id__c==null)

      l.Lead_Id__c = l.id;

update l;

}

}


      

}

Rahul SharmaRahul Sharma

Ashan, Code is not bulk enabled.

And also it wont work if any string is entered in Lead_Id__c, as you are checking just null condition.

 

check if this works.

 

trigger LeadAfterInsert on Lead (after insert, after update) 
{
	List<Lead> lstLeadstoupdate = new List<Lead>();
	if(trigger.isInsert)
	{
	    for(Integer i = 0; i < Trigger.new.size(); i++)
	    {
	    	String str = string.valueof(Trigger.new[i].Id);
	        lstLeadstoupdate.add(new Lead(Id = Trigger.new[i].Id, Lead_Id__c = str.substring(0, 15)));
	    }
	}
	else
	{
	    for(Integer i = 0; i < Trigger.new.size(); i++)
	        if(Trigger.old[i].Lead_Id__c != null && Trigger.new[i].Lead_Id__c !=  Trigger.old[i].Lead_Id__c)
	        {
	        	String str = string.valueof(Trigger.new[i].Id);
	            lstLeadstoupdate.add(new Lead(Id = Trigger.new[i].Id, Lead_Id__c = str.substring(0, 15)));
	        }
	        else if(Trigger.old[i].Lead_Id__c == null)
	        {
	        	String str = string.valueof(Trigger.new[i].Id);
	            lstLeadstoupdate.add(new Lead(Id = Trigger.new[i].Id, Lead_Id__c = str.substring(0, 15)));
	        }
	
	}
	
	if(!lstLeadstoupdate.isEmpty())
	    update lstLeadstoupdate;
}

 

This was selected as the best answer
tgk1tgk1

Your code worked perfect Raul!  Thanks!!  I hate to ask you for any more help but I'm having difficulty making a test class for this.  Can you please assist?  If not I totally understand, thanks again for everything.

 

 

Rahul SharmaRahul Sharma

You just need to create an Lead record with required fields, with this insert part of trigger will be covered.

And for update, just update Lead_ID__c as null. taadaa, whole trigger is covered.

 

Posting a test class for you which will cover above trigger:

@isTest
private class Test_LeadAfterInsert_Trigger {

    static testMethod void myUnitTest() 
    {
        // TO DO: implement unit test
        Test.startTest();
        	Lead objLead = new Lead();
        	objLead.LastName = 'Test Lead 1';
        	objLead.Company = 'Test Compant 1';
        	Insert objLead;
        	objLead.Lead_Id__c = '';
        	update objLead;
        Test.stopTest();
    }
}

 

tgk1tgk1

Worked perfect, thanks so much!  Have a great day!