You need to sign in to do that
Don't have an account?

Apex exception help
Hey guys, I'm having an issue with an apex trigger that I rolled out recently. The trigger is supposed to store a lead record's 18 digit id inside of a field I created called "lead_id__c" after save/insert. The trigger works fine but is throwing an exception if a lead owner is changed (i.e. it will throw the exception if I change the lead from sales rep a to sales rep b). Here's the error:
Apex script unhandled trigger exception by user/organization: 005000000073fWI/00D00000000hg2v LeadAfterInsert: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00Q00000009XfaeEAC; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: [] Trigger.LeadAfterInsert: line 29, column 9
Anybody have any idea how I can fix this? Here's the existing trigger and test class I have, I would really appreciate the help!
Trigger -
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, 18)));
}
}
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, 18)));
}
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, 18)));
}
}
if(!lstLeadstoupdate.isEmpty())
update lstLeadstoupdate;
}
Test class -
@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();
}
}
I think you pasted the wrong text into your "Trigger" box.
Could you please paste the Trigger there? =)
Hello,
Please post the trigger code here.
Sorry about that guys! Here's the trigger:
Hello,
Well there is no any issue with your trigger but i think you have to consider before update insted of after insert. Like this:
trigger LeadAfterInsert on Lead (after insert, before 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, 18)));
}
}
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, 18)));
}
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, 18)));
}
}
if(!lstLeadstoupdate.isEmpty())
update lstLeadstoupdate;
}
Think this will help you.
Why are you trying to store the Lead Id?
What's the use case?
To copy it across to Account when you convert it?
Thanks for your reply, I just tried this out. It compiles correctly but when you go to create a lead record I get this error:
Hi Mike - the use case is that my organization is trying to create an external database to house all of our Salesforce standard object data. The 18 digit lead ID needs to be mapped to a unique opportunity (one-to-one) so our marketing department can have better insight as to what's happening with our individual leads.
Hello,
Well i have gone through the Trigger and found that there is recursive calling of trigger i.e. when you insert a lead you are upadating also few fields inside that that causes your update trigger to fire also. So you have to handled recursive calling or your Trigger while inserting a lead.
Please go through these link to handle this:
http://developer.force.com/cookbook/recipe/controlling-recursive-triggers
Think this will help you.
Thanks ashish! Can you please help me with the modifications of the trigger/code? I'm not that great at Apex (somebody on the boards actually helped me out with the trigger / test class) and could really use some help.
1) Seems like you would just use the ID to populate this external database, instead of creating a new text field to hold a value that you already have in the ID.
2) You can create a formula field to reflect the record ID instead of messing around with a trigger.
3) ID values and strings have a special relationship in Apex. You don't need to use String.ValueOf(ID). There's and automatic conversion back and forth.