Activity Forums Salesforce® Discussions 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);

    }

    }

  • vivek

    Member
    February 9, 2022 at 11:59 am

    Good one William.. i got what i was looking for. Great Learning/ Thankx

  • vivek

    Member
    February 9, 2022 at 12:00 pm

    public class lead2webconvertHandler {

    public static void leadInsert(List<lead> newRecords){
    try {

    list<database.leadconvert> listLeadConverts = new List<database.leadconvert>();
    Database.LeadConvert leadObject = new Database.LeadConvert();
    List<integer> IntList = new List<integer>{1,2,3};

    for (Lead leads : newRecords) {
    system.debug(leads);
    if (leads.isConverted == false)
    {

    if(leads.LeadSource=='Web' && leads.Amount__c >=0)
    {
    leadObject.setLeadId(leads.Id);
    leadObject.setOpportunityName(leads.Name);
    leadObject.setConvertedStatus('Closed - Converted');
    listLeadConverts.add(leadObject);
    }
    else{
    if(leads.LeadSource=='Web')
    {
    leadObject.setLeadId(leads.Id);
    leadObject.donotcreateopportunity = True;
    leadObject.setConvertedStatus('Closed - Converted');
    listLeadConverts.add(leadObject);
    }
    }
    }
    }
    List<database.leadconvertresult> lcr = Database.convertLead(listLeadConverts);
    System.debug('Done!!');

    }catch (Exception e)
    {
    System.debug('Exception hit'+e.getMessage());
    }
    }

    }</database.leadconvertresult></integer></integer></database.leadconvert></database.leadconvert></lead>

  • CRMJetty

    Member
    March 25, 2022 at 10:50 am

    Hello,
    The convertLead Database method converts a lead into an account and contact or an account and person account, as well as (optionally) an opportunity. The convertLead takes an instance of the Database.LeadConvert class as a parameter. Create an instance of this class and set the information required for conversion, such as setting the lead, and destination account and contact.
    Refer to this:
    https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_convertlead.htm
    https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_dml_convertLead.htm
    Hope this helps you.
    Thanks

  • anikaa

    Member
    August 18, 2022 at 9:14 am

    Salesforce Consulting companies can help organizations to convert Lead in Salesforce Apex. Salesforce has a variety of automation and productivity tools, which are based upon the salesforce apex. This application is used for sales and support organizations. The Salesforce Sales and Service cloud computing solution provides a great way of turning the lead over to the sales force in a timely manner. Our salesforce consultants are highly trained and experienced in salesforce. We are one of the best salesforce consultancy firms . We provide Apex programming and salesforce technical support to customers across the globe. We enable customers to implement Salesforce with ease through our salesforce services. To know more about our top salesforce consultants. Visit us to get information about our top experts.

Log In to reply.

Popular Salesforce Blogs