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 

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();
    }
}



 



spraetzspraetz

I think you pasted the wrong text into your "Trigger" box.

 

Could you please paste the Trigger there? =)

ashish raiashish rai

Hello,

    Please post the trigger code here.

tgk1tgk1

Sorry about that guys!  Here's the 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;
}

 

ashish raiashish rai

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.

MikeGillMikeGill

Why are you trying to store the Lead Id?

 

What's the use case?

 

To copy it across to Account when you convert it?

 

 

tgk1tgk1

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:

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger LeadAfterInsert caused an unexpected exception, contact your administrator: LeadAfterInsert: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 00QP0000002csEKMAY; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LeadAfterInsert: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00QP0000002csEKMAY; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00QP0000002csEK) is currently in trigger LeadAfterInsert, therefore it cannot recursively update itself: [] Trigger.LeadAfterInsert: line 29, column 9: []: Trigger.LeadAfterInsert: line 29, column 9
 * = Required Information
Lead Information

 

tgk1tgk1

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. 

ashish raiashish rai

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.

 

tgk1tgk1

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.

JPClark3JPClark3

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.