Sub topic 7
Converting a Lead: What Gets Created and What Changes
This chapter assumes you can navigate Setup and the Object Manager, know the standard Account, Contact, and Opportunity objects, and can read basic Apex and SOQL. Earlier chapters covered Lead Status and assignment. Here you follow a lead through the moment it stops being a lead.
What conversion means
Conversion is the step where a sales rep decides a lead is qualified. Salesforce uses the lead's data to build the records that the rest of the sales process runs on. A single conversion can produce up to three new records:
- An Account, which represents the company.
- A Contact, which represents the person, and which is linked to that account.
- An Opportunity, which represents a potential deal. It is optional.
The lead is not deleted. It stays in the database as a converted lead, and it keeps pointers to the records it produced. That is why you can report on conversions later.
What gets created
Account
By default, Salesforce creates an account whose name comes from the lead's Company field. Other mapped fields, such as address and industry, are copied across. You can instead choose an existing account during conversion. Matching is covered in a later chapter, so this chapter only creates new records.
Contact
The contact receives the person's name, email, phone, and other mapped fields. It is attached to the account from the same conversion.
Opportunity
An opportunity is created unless the converter selects the option to not create one. In the API this is setDoNotCreateOpportunity(true). When one is created:
- Its name defaults to the account name, and the converter can change it.
- The new contact is added as a contact role on the opportunity.
- Its stage, close date, and other required fields come from your opportunity defaults, record types, and field mapping. Required opportunity fields with no value can make conversion fail.
Skipping the opportunity is common when you only want to record that a person is a real prospect, or when a deal already exists.
What changes on the lead
A successful conversion changes the lead record in these ways:
| Item | Before conversion | After conversion |
|---|---|---|
| `IsConverted` | false | true |
| Lead Status | An open status such as Working | A status that is marked as converted |
| `ConvertedDate` | Empty | The date of conversion |
| `ConvertedAccountId` | Empty | Id of the account created or used |
| `ConvertedContactId` | Empty | Id of the contact created or used |
| `ConvertedOpportunityId` | Empty | Id of the opportunity, or empty if none was created |
| Editing | Normal | Locked for ordinary users |
Converted leads are generally not editable, so fix data problems before you convert. There is no standard feature to undo a conversion. If you convert by mistake, you have to clean up the new records by hand.
The converted Lead Status must be one of the status values flagged as converted in Setup. If none is flagged, no conversion can complete. This is why the Apex examples below look up a status where IsConverted = true.
What moves to other records
Conversion also relocates related data:
- Open and closed activities (tasks and events) are associated with the new contact, so the history follows the person.
- Campaign member history is carried over to the contact.
- Notes and attachments stay linked to the converted records according to the conversion options and your org's settings.
Check how your org handles each of these in a sandbox before you promise a business team that a given item will move. Behavior depends on features you have enabled.
The owner of the new records
The converter picks an owner for the new account, contact, and opportunity. The default is the lead's owner. If the lead is owned by a queue, a user must be chosen, because queues cannot own accounts, contacts, or opportunities. In Apex, set the owner with setOwnerId. The converter can also choose to send an email notification to the new owner.
Converting from the user interface
The steps in Lightning Experience are:
- Open the lead and click Convert in the Path or the action menu.
- Choose to create a new account or select an existing one.
- Choose to create a new contact or select an existing one.
- Decide whether to create an opportunity, and edit its name if you do.
- Choose the record owner, and pick a converted Lead Status.
- Click Convert.
Salesforce then shows the new records. The user needs the right permissions, which a later chapter describes.
Converting with Apex
The Database.convertLead method performs the same conversion from code. Each call takes a Database.LeadConvert object. The only required values are the lead Id and a converted status.
LeadStatus convertStatus = [
SELECT MasterLabel
FROM LeadStatus
WHERE IsConverted = true
LIMIT 1
];
Lead l = [
SELECT Id
FROM Lead
WHERE IsConverted = false AND Email = '[email protected]'
LIMIT 1
];
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(l.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(true);
Database.LeadConvertResult result = Database.convertLead(lc);
System.debug('Success: ' + result.isSuccess());
System.debug('Account: ' + result.getAccountId());
System.debug('Contact: ' + result.getContactId());Because setDoNotCreateOpportunity(true) is set, result.getOpportunityId() returns null here.
Converting in bulk
Calling Database.convertLead once per lead inside a loop wastes DML limits. Pass a list instead, and set allOrNone to false so that one bad lead does not block the rest.
LeadStatus convertStatus = [
SELECT MasterLabel
FROM LeadStatus
WHERE IsConverted = true
LIMIT 1
];
List<Database.LeadConvert> converts = new List<Database.LeadConvert>();
for (Lead l : [SELECT Id FROM Lead WHERE IsConverted = false AND LeadSource = 'Web' LIMIT 100]) {
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(l.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
converts.add(lc);
}
List<Database.LeadConvertResult> results = Database.convertLead(converts, false);
for (Database.LeadConvertResult r : results) {
if (!r.isSuccess()) {
for (Database.Error err : r.getErrors()) {
System.debug('Conversion failed: ' + err.getMessage());
}
}
}Here an opportunity is created for each lead, because the do-not-create option was not set.
Verifying the result
Converted leads remain queryable, and they carry the ids of what they produced:
SELECT Id, Name, ConvertedDate, ConvertedAccountId, ConvertedContactId, ConvertedOpportunityId
FROM Lead
WHERE IsConverted = true
ORDER BY ConvertedDate DESC
LIMIT 10Use these fields to join a lead back to its account, contact, and opportunity in reports and in code.
Common failure causes
Conversion fails as a whole when one of the records it creates cannot be saved. Typical causes:
- No converted status. Setup has no Lead Status value flagged as converted, or the status passed in does not exist.
- Validation rules. Rules on Account, Contact, or Opportunity run against the new records. A rule that expects a field the lead did not map will block conversion.
- Required fields. A required custom field on the target object has no mapped value and no default.
- Missing permissions. The user lacks the Convert Leads permission or Create access to one of the target objects.
- Queue ownership. The chosen owner of the new records is a queue.
- Already converted. The lead has
IsConverted = true, so it cannot be converted again.
Triggers and flows also run. Conversion updates the lead and inserts the new records, so automation on all four objects fires. Test with realistic data, not only with a minimal lead.
Exercise
A rep converts a lead that is still owned by a queue, and the conversion fails. Explain why, and name two ways to fix it.
Show answer
Queues cannot own accounts, contacts, or opportunities, and conversion has to give the new records an owner. Fix it either by choosing a user as the record owner during conversion (in Apex, call setOwnerId with a user Id), or by assigning the lead to a user before converting it.
Exercise
After a successful conversion in Apex, what do result.getOpportunityId() and the lead's ConvertedOpportunityId contain if setDoNotCreateOpportunity(true) was used, and why can you still query the lead?
Show answer
Both are null, because no opportunity was created. The lead can still be queried because conversion does not delete it. It stays as a record with IsConverted = true and holds the ids of the account and contact it produced.