Activity Forums Salesforce® Discussions How can we convert Lead to Account through apex code? Reply To: How can we convert Lead to Account through apex code?

  • Parul

    September 21, 2018 at 12:13 pm

    When there is a chance of further negotiations with a lead, it can be converted into an account, contact,optionally an opportunity. In short, once the lead status has reached a certain stage, it can be qualified as a potential. On conversion, all the lead details are transferred in creating an account, contact and optionally an opportunity. The lead conversion process is a manual process, if you want to automate it then you have to use Apex code. This article will help you to understand how to automate lead conversion process using the Process Builder.

    Now, we have to understand a new Apex annotation i.e. @InvocableMethod. This annotation lets us use an Apex method as being something that can be call from somewhere other than Apex. The AutoConvertLeadsclass contains a single method that is passing the ids of the Leads whose Rating changed to Hot. Create the following class in your organization.

    Public class AutoConvertLeads
    public static void LeadAssign(List<Id> LeadIds)
    Database.LeadConvert Leadconvert = new Database.LeadConvert();
    LeadStatus Leads= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion
    Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);