Custom Lead Conversion In Salesforce

What is a Lead ?

Leads basically are people who are interested in your products or services. Leads may be an individual person or a company having interest in your goods.

How Leads are generated ?

Leads can be generated in many ways like from existing customers or advertisements etc. Or if you meet someone in person who is interested in your products, services. Leads also can be generated through social sites. Also we can provide descriptions about our products or services on our website. For increasing the number of leads, we can also provide discounts on our products so that people are willing to buy products.

How Leads are stored in Salesforce ?

In Salesforce, Information related to Leads are stored in Lead records.

Lead Conversion

When are lead converted ?

Essentially, leads are converted when they become our customer. Converting a lead creates a contact, along with an account and optionally, opportunity. But the lead should be qualified before it is converted!

Standard Lead Conversion

When leads are converted manually then the lead conversion process is called ‘Standard Lead Conversion’. In this process, leads are converted one by one in a manual way. There is a standard button on the lead record that converts that lead into account, contact and optionally opportunity. This is a very time consuming process. So we need Custom Lead Conversion that leverages the Apex Code.

Limitation of Standard Lead Conversion

  • Leads have to be converted individually.
  • It is a time consuming process.
  • Lead conversion is a manual conversion process.

Let’s see how the lead is converted using standard way -

Lead-Conversion-In-Salesforce.png
Figure 1: Showing convert button on lead record

To overcome the limitations stated above, we can write custom logic using apex code to automatically convert leads when they are qualified and create the account, contact and optionally an opportunity.

Custom Lead Conversion

We can leverage the Apex Code or the powerful Salesforce automation tool Process Builder to write our own logic to convert leads. Here, we are going to explain the custom lead conversion process using a simple trigger that is fired when we insert or update a lead record or bulk lead records. The trigger converts the lead into account and contact and opportunity (optionally) objects in Salesforce. A trigger is basically a block of code which automatically fires when an event occurs like before insert, before update, after insert, after update, before delete etc.

Advantages of Custom Lead Conversion

  • It provides bulk conversion of Leads.
  • It is a very fast process.
  • It doesn’t require any manual work.

Here is our trigger called ‘TriggerOnConvertLeads’ that we have written to explain custom lead conversion that automatically converts the lead when the lead’s status is changed to ‘Qualified’ i.e. when the lead is qualified to become the potential customer.

TriggerOnConvertLeads Trigger

trigger TriggerOnConvertLeads on Lead (after insert, after update) { List<lead> leadList = trigger.new; Set<ID> leadIds= new Set<ID>(); for(lead leadRecord : leadList) { if (leadRecord.Status == ‘Qualified’ && leadRecord.isConverted == false){ leadIds.add(leadRecord.Id); } //system.debug('size'+ leadIds.size()); if(leadIds.size() > 0){ ConvertLeads.LeadAssign(leadIds); } }

ConvertLeads class

Public class ConvertLeads { public static void LeadAssign(Set<Id> LeadIds) { LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1]; List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>(); for(id currentlead : LeadIds){ Database.LeadConvert Leadconvert = new Database.LeadConvert(); Leadconvert.setLeadId ( currentlead ); Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel); Leadconvert.setDoNotCreateOpportunity(TRUE); //you can remove this line if you want to create an opportunity during conversion MassLeadconvert.add(Leadconvert); } if (!MassLeadconvert.isEmpty()) { List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert); } }}

This trigger converts simultaneously all the leads which you want to convert . By doing so, you can convert bulkified lead records to account object , contact object , opportunity object (optionally). If you don't want to create an opportunity for the lead records (which you want to convert), then you can do it by setting setDoNotCreateOpportunity to True.

Database.convertLead() is a standard method that automatically converts leads to an account , contact and opportunity(optionally). The Database.convertLead() method can take one LeadConvert object or a list of LeadConvert objects.

This way, you can convert bulkified lead records in a few minutes saving your precious time.

There are a number of resources available that can help you understand the process easily. Some of them are listed below for your reference :

For any query on Lead Conversion, contact support@astreait.com