The convertLead DML operation converts a lead into an account and contact, as well as (optionally) an opportunity.
The optional opt_allOrNoneparameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why.
When converting leads, consider the following rules and guidelines:
Converting leads involves the following basic steps:
SELECT Id, Name FROM Account WHERE Name='CompanyNameOfLeadBeingMerged'SELECT Id, Name FROM Contact WHERE FirstName='FirstName' AND LastName='LastName' AND AccountId = '001...'
The convertLead database method accepts up to 100 LeadConvert objects. A LeadConvert object supports the following methods:
| Name | Arguments | Return Type | Description |
|---|---|---|---|
| getAccountId | ID | Gets the ID of the account into which the lead will be merged. | |
| getContactId | ID | Gets the ID of the contact into which the lead will be merged. | |
| getConvertedStatus | String | Get the lead status value for a converted lead | |
| getLeadID | ID | Get the ID of the lead to convert. | |
| getOpportunityName | String | Get the name of the opportunity to create. | |
| getOwnerID | ID | Get the ID of the person to own any newly created account, contact, and opportunity. | |
| isDoNotCreateOpportunity | Boolean | Indicates whether an Opportunity is created during lead conversion (false, the default) or not (true). | |
| isOverWriteLeadSource | Boolean | Indicates whether the LeadSource field on the target Contact object is overwritten with the contents of the LeadSource field in the source Lead object (true), or not (false, the default). | |
| isSendNotificationEmail | Boolean | Indicates whether a notification email is sent to the owner specified by setOwnerId (true) or not (false, the default). | |
| setAccountId | ID ID | Void | Sets the ID of the account into which the lead will be merged. This value is required only when updating an existing account, including person accounts. Otherwise, if setAccountID is specified, a new account is created. |
| setContactId | ID ID | Void | Sets the ID of the contact into which the lead will be merged (this contact must be associated with the account specified with setAccountId, and setAccountId must be specified). This value is required only when updating an existing contact.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). |
| setConvertedStatus | String Status | Void | Sets the lead status value for a converted lead. This field is required. |
| setDoNotCreateOpportunity | Boolean CreateOpportunity | Void | Specifies whether to create an opportunity during lead conversion. The default value is false: opportunities are created by default. Set this flag to true only if you do not want to create an opportunity from the lead. |
| setLeadId | ID ID | Void | Sets the ID of the lead to convert. This field is required. |
| setOpportunityName | String OppName | Void | Sets the name of the opportunity to create. If no name is specified, this value defaults to the company name of the lead. 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. |
| setOverwriteLeadSource | Boolean OverwriteLeadSource | Void | Specifies whether to overwrite the LeadSource field on the target contact object with the contents of the LeadSource field in the source lead object. The default value is false, to not overwrite the field. If you specify this as true, you must also specify setContactId for the target contact. |
| setOwnerId | ID ID | Void | Specifies the ID of the person to own any newly created account, contact, and opportunity. If the application does not specify this value, the owner of the new object will be the owner of the lead. 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. |
| setSendNotificationEmail | Boolean SendEmail | Void | Specifies whether to send a notification email to the owner specified by setOwnerId. The default value is false, that is, to not send email. |
An array of LeadConvertResult objects is returned with the convertLead database method. Each element in the LeadConvertResult array corresponds to the SObject array passed as the SObject[] parameter in the convertLead database method, that is, the first element in the LeadConvertResult array matches the first element passed in the SObject array, the second element corresponds with the second element, and so on. If only one SObject is passed in, the LeadConvertResults array contains a single element.
A LeadConvertResult object has the following methods:
| Name | Type | Description |
|---|---|---|
| getAccountId | ID | The ID of the new account (if a new account was specified) or the ID of the account specified when convertLead was invoked |
| getContactId | ID | The ID of the new contact (if a new contact was specified) or the ID of the contact specified when convertLead was invoked |
| getErrors | Database.Error []Database.Error [] | If an error occurred, an array of one or more database error objects providing the error code and description. For more information, see Database Error Object Methods. |
| getLeadId | ID | The ID of the converted lead |
| getOpportunityId | ID | The ID of the new opportunity, if one was created when convertLead was invoked |
| isSuccess | Boolean | A Boolean value that is set to true if the DML operation was successful for this object, false otherwise |
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());