4

I've written a trigger to auto convert leads coming in from a web form. When I send the information from the web form, the Lead is created, the trigger runs the Database.convertLead and I get an Account, Contact and Opportunity. However looking at the Lead tab I see the Lead I just entered, it was not converted, or at least it was not updated as IsConverted.

Trigger:

Trigger AllLeadTrigger on Lead (after insert)
{

    List<Lead> autoConvertLeadList = new List<Lead>();
    for (Lead l : Trigger.New)
    {
        if (l.LeadSource == 'Partner Webform' || l.LeadSource == 'Employer Webform')
        {
            autoConvertLeadList.add(l);
        }
    }
    if (!autoConvertLeadList.isEmpty())
    {
        AllLeadTriggerHelper.ConvertLeads(autoConvertLeadList);
    }

}

Class:

public class AllLeadTriggerHelper
{

    public static void ConvertLeads (List<Lead> leadList)
    {
        if (leadList.isEmpty())
        {
            return;
        }


        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=True LIMIT 1];




        List<Database.LeadConvert> lcList = new List<Database.LeadConvert>();
        for (Lead l : leadList)
        {
            if (l.IsConverted)
            {
                continue;
            }

            Database.LeadConvert lc = new Database.LeadConvert();

            lc.setLeadId(l.Id);

            String opName = l.Company==null?'':l.Company
                            + ' - '
                            + l.FirstName==null?'':l.FirstName
                            + ' '
                            + l.LastName==null?'':l.LastName;
            opName = opName.left(120).trim();
            lc.setOpportunityName(opName);

            lc.setConvertedStatus(convertStatus.MasterLabel);

            lcList.add(lc);
        }


        List<Database.LeadConvertResult> lcrList;
        if ( ! lcList.isEmpty())
        {
            lcrList = Database.convertLead(lcList, False);
        }
        for (Database.LeadConvertResult lcr : lcrList)
        {
            System.Debug('@@@@@@@@'+lcr);
        }

    }


}

When I check the debug log I see the leadConvert was a success, records are created but the Lead is not closed/converted.

21:45:08.004 (1004171117)|USER_DEBUG|[52]|DEBUG|@@@@@@@@Database.LeadConvertResult[getAccountId=001m0000009zLtNAAU;getContactId=003m0000007zEppAAE;getErrors=();getLeadId=00Qm00000026BDyEAM;getOpportunityId=006m0000002gMjlAAE;isSuccess=true;]

When I run this Query

SELECT Company, ConvertedAccountId, ConvertedContactId, ConvertedDate, 
       ConvertedOpportunityId, FirstName, IsConverted, LastName 
FROM Lead

I just see blanks in the 'converted' fields for the Lead.

If I key in a Lead in the Salesforce screen, and set the LeadSource to one of the values the trigger expects then it auto converts exactly as I would expect. This just seems to happpen to web-to-lead Leads.

[I have tried deactivating all workflows and also changing the class/trigger api from 32 to 28 but it made no difference]

1

I followed the advice in the below link, that I make the class method called from the trigger a @future method. I guess web-to-lead is doing some other stuff after the trigger runs, so converting it in a different context is the way to go.

https://developer.salesforce.com/forums?id=906F0000000AfdeIAC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.