How to Automatically Convert Leads in Apex

Sharing is Caring

In the blog post “What’s the difference between a Lead and Opportunity?” I spent a bit of time covering how leads are converted into accounts, contacts, and opportunities.

Basically, a lead is converted when the sales person thinks that there’s actually an opportunity to sell them something. In a lot of cases, it might make sense of for leads to be automatically converted after a field is set to a certain value. For example, a lot of companies might want to automatically convert a Lead as soon as the Sales Rep has completed ANUM or BANT.

To convert a Lead into an Account, Contact and an Opportuninity we need to make use of the Database.convertLead method. The Database.convertLead method requires an instance of the Database.LeadConvert method which appears to be relatively simple to us.

Here’s a very simple example of everything we have covered so far

	// Create a lead and make sure it's been inserted because we need the Id.
	Lead l = new Lead(FirstName = 'Brian', LastName = 'Cline', Company = 'my Company');
	insert l;
	
	Database.LeadConvert lc = new Database.LeadConvert();
	lc.setLeadId(l.Id);

	Database.convertLead(lc);

As the code above is currently written, it would fail because the Lead Converted Status needs to be set. So the code needs to be expanded to the following:

	// Create a lead and make sure it's been inserted because we need the Id.
	Lead l = new Lead(FirstName = 'Brian', LastName = 'Cline', Company = 'my Company');
	insert l;
	
	Database.LeadConvert lc = new Database.LeadConvert();
	lc.setLeadId(l.Id);
	
	LeadStatus ls = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
	lc.setConvertedStatus(ls.MasterLabel);
	
	Database.convertLead(lc);

The only problem with the previous code is that we still don’t know whether it’s actually been successful or not. Don’t worry Salesforce provides a way to know if it’s been successful.

We can simply change the code to now use “Database.LeadConvertResult” which Database.convertLead() actually returns and check the value of the isSuccess() method.

	// Create a lead and make sure it's been inserted because we need the Id.
	Lead l = new Lead(FirstName = 'Brian', LastName = 'Cline', Company = 'my Company');
	insert l;
	
	Database.LeadConvert lc = new Database.LeadConvert();
	lc.setLeadId(l.Id);
	
	LeadStatus ls = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
	lc.setConvertedStatus(ls.MasterLabel);
	
	Database.LeadConvertResult lcr = Database.convertLead(lc);
	boolean isSuccessful = lcr.isSuccess();
	
	if (isSuccessful) {
		//doSomethingElse
	}

The most likely place to automatically convert a lead is in an apex Lead after trigger, so this example doesn’t exactly work because it isn’t bulkified. Thankfully Salesforce has actually provided some bulkified methods.

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.