Activity Forums Salesforce® Discussions How to convert lead using Salesforce Apex?

  • Aman

    Member
    September 22, 2018 at 9:50 pm

    How to convert lead using Salesforce Apex?

  • shariq

    Member
    September 22, 2018 at 9:50 pm

    Lead myLead = new Lead(LastName = 'Foo', Company='Foo Bar');
    insert myLead;

    Database.LeadConvert lc = new database.LeadConvert();
    lc.setLeadId(myLead.id);

    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    lc.setConvertedStatus(convertStatus.MasterLabel);

    Database.LeadConvertResult lcr = Database.convertLead(lc);
    System.assert(lcr.isSuccess());

  • Parul

    Member
    September 23, 2018 at 5:04 am

    Public class LeadClass{
    public static void doConvert(Id leadId){

    Database.LeadConvert lc = new database.LeadConvert();
    lc.setLeadId(leadId);
    lc.setDoNotCreateOpportunity(True);

    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    lc.setConvertedStatus(convertStatus.MasterLabel);
    Database.LeadConvertResult lcr = Database.convertLead(lc);
    }
    }

     

    Thanks

  • William

    Member
    May 9, 2019 at 7:19 am

    trigger leadConversation on Lead (before update) {

    LeadStatus convertStatus = [select MasterLabel from LeadStatus where IsConverted = true limit 1];

    List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

    for (Lead lead: Trigger.new) {

    if (!lead.isConverted && lead.status== 'Closed - Converted') {

    Database.LeadConvert lc = new Database.LeadConvert();

    String oppName = lead.Name;

    lc.setLeadId(lead.Id);

    lc.setOpportunityName(oppName);

    // Leadconvert.setDoNotCreateOpportunity(TRUE);

    lc.setConvertedStatus(convertStatus.MasterLabel);

     

    leadConverts.add(lc);

    }

    }

    if (!leadConverts.isEmpty()) {

    List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);

    System.debug(+'result'+lcr);

    }

    }

Viewing 1 - 4 of 4 posts

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos