Skip to content

Instantly share code, notes, and snippets.

@jkentjnr
Created October 19, 2023 11:01
Show Gist options
  • Save jkentjnr/8edc552a9a4b2753ee3a1fe90ba75e3d to your computer and use it in GitHub Desktop.
Save jkentjnr/8edc552a9a4b2753ee3a1fe90ba75e3d to your computer and use it in GitHub Desktop.
Salesforce_Lead_Convert
// Set lead Id here.
Id leadId = '00Q8t0000024px3EAA';
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1];
String leadStatus = convertStatus.MasterLabel;
// Execute Salesforce' lead convert service
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(leadId);
lc.setConvertedStatus(leadStatus);
Database.LeadConvertResult lcr = Database.convertLead(lc);
// Report an errors
List<Object> convertErrorList;
if (lcr.isSuccess() == false) {
convertErrorList = new List<Object>();
for (Database.Error err : lcr.getErrors()) {
convertErrorList.add(new Map<String, Object> {
'statusCode' => err.getStatusCode(),
'message' => err.getMessage(),
'fields' => err.getFields()
});
}
}
// Log the lead convert results
System.Debug(new Map<String, Object> {
'errors' => convertErrorList,
'contactId' => lcr.getContactId(),
'accountId' => lcr.getAccountId(),
'originalLeadId' => leadId,
'success' => lcr.isSuccess()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment