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

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
Post a Comment