Bulk Lead Conversion

  






For Creating bulk lead Records follow below notes and code sniffet.

Database.LeadConvert class:

A lead is transformed into an account and contact or an account and person account, as well as (optionally) an opportunity, using the Database.LeadConvert


It Has four class methods follow below notes



convertLead() changes a Lead into a different kind of object.
transforms a lead into an opportunity, contact, or account (optionally).

LeadConvertResult Class:

When using the convertLead Database method, an array of LeadConvertResult objects is returned. Each element in the LeadConvertResult array matches an element passed in the SObject array, the second element in the LeadConvertResult array matches the second element, and so on. This is because each element in the LeadConvertResult array corresponds to the sObject array passed as the SObject[] parameter in the convertLead Database method. The LeadConvertResult array has a single entry if a single sObject is supplied.






UseCase :

Create a trigger to auto-convert the lead records as the customers upon changing the lead status as "Closed - Converted"

Object Name-Lead 

Event= After Update

 prerequisite :create checkbox on lead object as  Do Not Create Opportunity


Trigger Code;

trigger LeadTrigger on Lead (After Update)

{

if(Trigger.isAfter && Trigger.isUpdate)

    {

       LeadTriggerHandler.autoLeadConvert(Trigger.new);

        

    }

}


LeadTriggerHandler code:


public class LeadTriggerHandler

{

public static void autoLeadConvert(List<Lead> lstLead)

    {

      List<Database.LeadConvert> lsConvert = new  List<Database.LeadConvert>();

       LeadStatus lsStatus = [select id, MasterLabel, isConverted From LeadStatus where isConverted = true];


        for(Lead ldRecord : lstLead) 

        {

            if(ldRecord.Status =='Closed - Converted' && ldRecord.IsConverted == false)

            {

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

                

                lconvert.setLeadId(ldRecord.Id);

                lconvert.setDoNotCreateOpportunity(ldRecord.Do_Not_Create_Opportunity__c);

                lconvert.setSendNotificationEmail(true);

                lconvert.setConvertedStatus(lsStatus.MasterLabel);

                

                //add the record to the collection

                

              lsConvert.add(lconvert);

               

            }

        }

        if(!lsConvert.isEmpty())

        {

            Database.LeadConvertResult[] result = Database.convertLead(lsConvert,false);

           

        }

    }

}




Comments