8

Lead Conversion | Method in Apex Database Class

What Is Lead Conversion In Salesforce?

Lead conversion in Apex is the process of converting a lead record into Accounts, Contacts & Opportunities. This happens when a lead is identified as a qualified Sales prospect.

Lead Conversion Using Apex Coding

There exists a Database class method, “convertLead in Salesforce Apex,” which converts a Lead in Salesforce Apex. In addition to this method, various variables and classes are used for Apex lead conversion, as shown in the sample code below.

Lead Conversion:
public static void main() {
    Lead l = [SELECT Name FROM Lead WHERE Name like’ sh % ’LIMIT 1];
    Lead l1 = new Lead(LastName = ‘Test’, Company = ‘Test Company’);
    insert l1;
    Database.LeadConvert lc = new Database.LeadConvert();
    lc.setLeadId(l.Id);
    System.debug(‘ID of record inserted is: ’+l.id);
    lc.setconvertedStatus (‘Closed Converted’);
    lc.setownerId = [SELECT Id FROM User LIMIT 1].Id; // any user id
    Database.LeadConvertResult lcr = Database.convertLead(lc);
if (lcr.isSuccess()) {
    System.debug('Lead converted successfully. Contact Id: ' + lcr.getContactId());
} else {
    System.debug('Lead conversion failed: ' + lcr.getErrors());
}
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Frequently Asked Questions

What is Database.convertLead in Apex?

Database.convertLead in Apex is a way to convert lead records in Salesforce programmatically. It allows the database.LeadConvert object with conversion settings, including LeadID, conversion status, and owner ID.

Can we automate lead conversion in Apex?

Yes, by using Database.covertLead(), in Apex, the whole conversion process can be automated.

Can we convert multiple leads in Apex?

Yes, Database.convertLead() accepts List

What is LeadConvertResult in Apex?

The Database.LeadConvertResult provides methods to check conversion status, retrieve errors, and retrieve the IDs of the created records.

Book Free15-Minutes Career Counselling