Automatically convert Lead to Contact using Apex Trigger

Automatically convert Lead to Account, Contact and Opportunity using Apex Trigger if the Lead Status = Open

For Example:
Here we declare our trigger in Lead Obejct and name it as LeadAutoConverter and it will fire only after an insert the lead record.

trigger LeadAutoContactConverter on Lead (after insert) {

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

List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
for (Lead lead: {
if (!lead.isConverted && lead.Status == 'Open' ) {
Database.LeadConvert lc = new Database.LeadConvert();
String opportunityName = lead.Name;

//lc.setAccountId(AccountId); // you can set the AccountId instead of create a new account and contact is created the mention account
//lc.setDoNotCreateOpportunity(true); // Optional to create a Opportunity
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);

Test Class:

private class TestLeadTrigger {
static testMethod void TestLeadTrigger() {
Lead l = new Lead(FirstName = 'FName', LastName = 'LName', Company = 'Test Account', Status = 'Open', Email='');
insert l;
system.assertEquals(l.FirstName, 'FName');


LeadConvert Class

Lead Validation Rule not working when converting Leads.
Resolution To enable the “Require validation for Converted Leads”
Go to your Name | Set-up | Customize | Leads | Settings
Enable “Require validation for Converted Leads”
Click on Save.

For information on Enabling ‘Use Apex Lead Convert‘,


Written by 

I am Sakthivel Madesh, a certified Salesforce Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 13x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply