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

  • Kumar

    Member
    February 10, 2017 at 11:29 am

    Hi Sushant,

    You can use the LeadConvert class in Salesforce for this scenario. Hope this helps.

  • madhulika shah

    Member
    September 6, 2018 at 7:30 am

    Hi,

    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.

    Public class AutoConvertLeads
    {
    @InvocableMethod
    public static void LeadAssign(List<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); //Remove this line if you want to create an opportunity from Lead Conversion
    MassLeadconvert.add(Leadconvert);
    }

    if (!MassLeadconvert.isEmpty()) {
    List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
    }
    }
    }

    Modify the above class as per your requirement.

    Thanks.

  • Parul

    Member
    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
    {
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
    Database.LeadConvert Leadconvert = new Database.LeadConvert();
    Leadconvert.setLeadId(LeadIds[0]);
    LeadStatus Leads= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    Leadconvert.setConvertedStatus(Leads.MasterLabel);
    Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion
    Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);
    System.assert(Leadconverts.isSuccess());
    }
    }

     

  • shariq

    Member
    September 22, 2018 at 3:05 am

    Hi,

    There is a standard LeadConvert class provided by salesforce which can be used in your scenario.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs