The convertLead Database method converts a lead into an account and contact, as well as (optionally) an opportunity. The convertLead takes an instance of the Database.LeadConvert class as a parameter. Create an instance of this class and set the information required for conversion, such as setting the lead, and destination account and contact.
This example shows how to use the Database.convertLead method to convert a lead. It inserts a new lead, creates a LeadConvert object, sets its status to converted, then passes it to the Database.convertLead method. Finally, it verifies that the conversion was successful.
Lead myLead = new Lead(LastName = 'Fry', Company='Fry And Sons'); insert myLead; Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(myLead.id); LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1]; lc.setConvertedStatus(convertStatus.MasterLabel); Database.LeadConvertResult lcr = Database.convertLead(lc); System.assert(lcr.isSuccess());
The following are methods for LeadConvert. All are instance methods.
public ID getAccountId()
Type: ID
public ID getContactId()
Type: ID
public String getConvertedStatus()
Type: String
public String getOpportunityName()
Type: String
public ID getOwnerID()
Type: ID
public Boolean isDoNotCreateOpportunity()
Type: Boolean
public Boolean isOverWriteLeadSource()
Type: Boolean
public Boolean isSendNotificationEmail()
Type: Boolean
public Void setAccountId(ID accountId)
Type: Void
public Void setContactId(ID contactId)
Type: Void
If setContactId is specified, then the application creates a new contact that is implicitly associated with the account. The contact name and other existing data are not overwritten (unless setOverwriteLeadSource is set to true, in which case only the LeadSource field is overwritten).
public Void setConvertedStatus(String status)
Type: Void
public Void setDoNotCreateOpportunity(Boolean createOpportunity)
Type: Void
public Void setLeadId(ID leadId)
Type: Void
public Void setOpportunityId(ID opportunityId)
Type: Void
public Void setOpportunityName(String opportunityName)
Type: Void
The maximum length of this field is 80 characters.
If setDoNotCreateOpportunity is true, no Opportunity is created and this field must be left blank; otherwise, an error is returned.
public Void setOverwriteLeadSource(Boolean overwriteLeadSource)
Type: Void
public Void setOwnerId(ID ownerId)
Type: Void
This method is not applicable when merging with existing objects—if setOwnerId is specified, the ownerId field is not overwritten in an existing account or contact.
public Void setSendNotificationEmail(Boolean sendEmail)
Type: Void