Sub topic 13
Troubleshooting and best practices
This chapter assumes you have read the earlier chapters on assignment rules, conversion, and duplicate matching. You should be comfortable with Setup, with the Lead, Account, Contact, and Opportunity objects, and with basic Apex and SOQL. Here you will learn to diagnose common failures and avoid them in the first place.
Diagnosing assignment problems
Most assignment problems come from one of three causes: the rule did not run, it ran but nothing matched, or it matched and assigned the lead to the wrong place.
Keep these facts in mind while you work through the path:
- Only one lead assignment rule can be active at a time.
- Entries are evaluated in sort order, and the first match wins. A broad entry placed too early hides every entry after it.
- A queue can own leads only if Lead is listed in the queue's supported objects.
- Editing a lead does not re-run the rule. Someone has to trigger it again.
Diagnosing conversion problems
Conversion creates several records in one transaction. If any step fails, the whole conversion fails.
| Symptom | Likely cause | Fix |
|---|---|---|
| "Converted status" error in Apex | No Lead Status value is marked as converted | Add or mark a converted status in Lead Status Picklist Values |
| Convert button is missing | User lacks the Convert Leads permission, or lacks Create on Account, Contact, or Opportunity | Adjust the profile or permission set |
| Required field error during conversion | A required or validation-checked field on Account, Contact, or Opportunity has no mapped value | Map the field, or relax the validation rule for the conversion |
| Duplicate error | A duplicate rule blocks the new Account or Contact | Convert into the existing record, or review the rule's action |
| Mapped value is blank on the new record | The lead field is empty, or its type does not match the target field | Check mapping and data types |
| Lead cannot be converted again | `IsConverted` is already true | Query `ConvertedAccountId` and related fields to find the records |
Best practices
- Keep rules simple. Use few entries with clear, mutually exclusive criteria. Put the most specific entries first and a catch-all entry last.
- Always set a default lead owner. Unmatched leads then land with a person or queue who monitors them.
- Prefer queues to individuals. Users leave and go on vacation. A queue keeps leads from becoming orphaned.
- Search before you convert. Train users to check for an existing account or contact. Rely on duplicate rules to catch the rest.
- Map fields deliberately. Review the mapping whenever you add a custom field to Lead that matters after conversion.
- Test in a sandbox with realistic data. Test with a restricted user as well as an admin, because permission problems rarely show up for admins.
Bulk-safe conversion in Apex
When you convert in Apex, pass a list of Database.LeadConvert objects and use partial success. One bad lead then does not roll back the others. A single call accepts up to 100 conversions.
String convertedStatus = [
SELECT MasterLabel
FROM LeadStatus
WHERE IsConverted = true
LIMIT 1
].MasterLabel;
List<Lead> leads = [
SELECT Id
FROM Lead
WHERE IsConverted = false AND Status = 'Qualified'
LIMIT 100
];
List<Database.LeadConvert> converts = new List<Database.LeadConvert>();
for (Lead l : leads) {
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(l.Id);
lc.setConvertedStatus(convertedStatus);
converts.add(lc);
}
List<Database.LeadConvertResult> results = Database.convertLead(converts, false);
for (Integer i = 0; i < results.size(); i++) {
if (!results[i].isSuccess()) {
for (Database.Error err : results[i].getErrors()) {
System.debug('Lead ' + leads[i].Id + ' failed: ' + err.getMessage());
}
}
}Read the debug log for the error messages, or write them to a custom log object. The messages usually name the field or rule that caused the failure.
Exercise
A Web-to-Lead form creates leads, but they all end up with the default lead owner even though you have an active assignment rule. Name two things to check.
Show answer
First, check the rule entry criteria and sort order. If no entry matches, the lead goes to the default lead owner, and a broad entry earlier in the order can also capture leads unexpectedly. Second, confirm the rule is the active one and that its assignees are valid, meaning the queue supports Lead and the user is active. If the entries are correct, check that the form submits the field values the criteria test, such as country or lead source.
Exercise
Why should Database.convertLead be called with allOrNone set to false when you convert many leads at once?
Show answer
With allOrNone set to false, each lead is converted independently. A failure on one lead, such as a validation or duplicate error, does not roll back the others. You can then inspect each LeadConvertResult to see which leads failed and why.