+ Start a Discussion
Nick MortensenNick Mortensen 

Trigger failing because it is trying to convert a lead that has already been converted

I am getting the following error when I try to automatically convert a web-to-lead lead.  I am running after update because I am using assignment rules to assign the lead to the appropriate person.  An help would be greatly appreciated!





Salesforce could not create this lead because of the reason listed below. For more information about this error or help with Web-to-Case-Lead, please contact Customer Support.

Reason: common.apex.runtime.TriggerExecutionException: Apex trigger AutoConvert caused an unexpected exception, contact your administrator: AutoConvert: execution of AfterUpdate

caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, System.DmlException: Update failed. First exception on row 0 with id 00QG000000l15kOMAQ; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: []

(System Code)
: []: Trigger.AutoConvert: line 9, column 1
    Lead Capture Page: Not available.







Here is the trigger I am using:

trigger AutoConvert on Lead (after update) {

    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];  
    for(Lead L : Trigger.New){     
        If(L.IsConverted == false){
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(L.id);
            lc.setConvertedStatus(convertStatus.MasterLabel);
            Database.LeadConvertResult lcr = Database.convertLead(lc); System.assert(lcr.isSuccess());
        }//end converted if statement
    }//end L for loop
}//end trigger


I am confused because It shouldn't try to convert a lead unless IsConverted is false.  What is even more strange is that this code work flawlessly in the sandbox.  Any ideas on what I can do to fix it?
Ramu_SFDCRamu_SFDC
See if this helps. I understand that you are already checking for isconverted=false however there is something else which needs to be considered as per that documentation

http://help.salesforce.com/apex/HTViewSolution?id=000002543&language=en_US


Vinita_SFDCVinita_SFDC
Hi,

This error is normally received when you try to modify or update a lead that has been converted.

It is also possible that you're trying to reference the Lead ID after it was converted.

Note that once a lead is converted - you can only reference the convertedAccountId or convertedContactId fields.