Salesforce Apex Convert Lead

In many of the business cases, you may want control lead conversion process through apex trigger or your may need to build your own lead conversion process . in this post, we are going to see how to control the lead conversion from the apex trigger.

What happens when the lead is converted?

  • A contact, account, and opportunity are created and populated with the lead’s data
    The lead field “Converted” is changed from False to True.
  • The data within standard lead fields is automatically transferred to the contact, account, and/or opportunity. For Custom Field use Lead mapping
  • In general on lead conversion, if you wanted to perform any custom logics like then you will go for apex lead conversion.
    Here are some of the use cases.
    1.Checking Account Industry and SIC mapping against your company mapping
    2.On lead Conversion, if you wanted to auto subscribe specific user the record for chatter feed
    3. Sending new or updated account information to ERP or etc.
    Here is the simple job that will convert leads. You can schedule this class to perform the automatic lead conversion
  global class LeadConversionJob implements Schedulable {
    global void execute(SchedulableContext sc){
        List<Lead> leads = [Select Id , Name,Company  from lead where LeadSource='Web' ];

        List<Database.LeadConvert> lcList = new List<Database.LeadConvert>();
        for(Lead l :leads){
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(l.id);
            lc.ConvertedStatus = 'Closed - Converted';
            lcList.add(lc);
        }
        List<Database.LeadConvertResult> lcr = Database.convertLead(lcList);      
        for(Database.LeadConvertResult lcrRes : lcr){
            if(lcrRes.isSuccess()){

            }else{
                //error handling here 
            }

        }

    }

}

If you would like to perform some of the operations on after lead is conversion or to perform some of the business requirements, you write apex trigger as shown below.

Apex Trigger:-

 trigger LeadConversion on Lead (after update) {

    LeadConversionHandler.convertLead(Trigger.newMap , Trigger.oldMap);

}

Apex Class

public class LeadConversionHandler {

    public static void convertLead(Map<Id, Lead> newLead , Map<Id ,Lead> oldLead){
        for(Id idNew :newLead.keySet()){
            If(newLead.get(idNew).isConverted== true &&  oldLead.get(idNew).isConverted== false){
                If(newLead.get(idNew).ConvertedAccountId!=null){
                    // Use your logics here
                    Account a = [Select a.Id, a.Description From Account a Where a.Id = :newLead.get(idNew).ConvertedAccountId];

                }     
                If(newLead.get(idNew).ConvertedContactId!=null){
                    // Use your logics here 
                    Contact c = [Select c.Id, c.Description, c.Name From Contact c Where c.Id = :newLead.get(idNew).ConvertedContactId];
                }
                If(newLead.get(idNew).ConvertedOpportunityId!=null){
                    // use your logics here 
                    Opportunity opp = [Select o.Id, o.Description from Opportunity o Where o.Id = :newLead.get(idNew).ConvertedOpportunityId];

                }
            } 

        }

    }

}