You need to sign in to do that
Don't have an account?
tgk1
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
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.
All Answers
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.
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!
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
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!
Modified a code a bit, check now, its no event, after update and adter insert.
Hope it helps :)
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!
tried in my org, work fine.
how are you updating lead records, which fields?
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;
}
}
}
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.
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.
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:
Worked perfect, thanks so much! Have a great day!