Monday, August 21, 2017

How to Convert Lead Using Apex

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.

// 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
}

No comments:

Post a Comment