Sub topic 13
Troubleshooting and best practices
This chapter assumes you can work in Setup and Object Manager and know the basics of Account, Contact, and Opportunity. It also assumes you have read the earlier chapters on assignment rules, conversion, field mapping, and Database.convertLead. The Apex section requires basic Apex and SOQL. The goal is a repeatable way to diagnose assignment and conversion problems, plus habits that prevent them.
A diagnostic approach
Most lead problems fall into three groups: the wrong owner, a conversion that fails, or data that lands in the wrong place after conversion. Start from the symptom and work backward.
| Symptom | Most likely cause | First check |
|---|---|---|
| Lead owner is the creating user or default owner | Rule did not run, or no entry matched | Was the rule triggered, and is it active? |
| Lead goes to the wrong user | Entry order, or a broader entry above the one you expected | Rule entry sort order |
| Lead cannot be assigned to a queue | Queue does not support the Lead object | Queue settings |
| No Convert button | Missing Convert Leads permission, or lead already converted | Profile or permission set, `IsConverted` |
| Conversion error about status | Status is not marked as converted | `LeadStatus` where `IsConverted = true` |
| Conversion error naming a field | Validation rule or required field on a target object | Validation rules on Account, Contact, Opportunity |
| Data not copied to the new records | Field not mapped, or incompatible types | Map Lead Fields |
| Unwanted duplicate contact | Existing record not selected | Matching and duplicate rules |
Troubleshooting assignment
The rule never ran
Assignment rules do not run on every save. They run when something asks for them:
- In the UI, the user must check Assign using active assignment rule, and the field must be on the page layout.
- Web-to-Lead runs the active rule automatically.
- The Data Import Wizard has an option to assign using the active rule.
- Apex and API inserts run no rule unless you pass assignment rule options.
If a lead created in Apex ends up owned by the running user, this is almost always the cause. Set useDefaultRule as shown below.
Lead l = new Lead(
FirstName = 'Grace',
LastName = 'Hopper',
Company = 'Compiler Works',
LeadSource = 'Web'
);
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
l.setOptions(dmo);
insert l;Set the options before the DML statement, on each record. Options set after the insert have no effect.
The rule ran but picked the wrong owner
Only one lead assignment rule can be active at a time. Within it, Salesforce evaluates entries in sort order and stops at the first match. Check these in order:
- Confirm the right rule is the active one.
- Open the entries and read them top to bottom as the platform would. A broad entry such as "Country equals United States" placed above "State equals California" shadows the narrower one.
- Check the field values on the lead. Criteria compare exact values, so
USAandUnited Statesare different. If you use state or country picklists, compare against the stored values. - Check whether an entry with a blank criteria field was intended as a catch-all.
If no entry matches, the lead falls to the default owner. For Web-to-Lead that is the default lead owner in Lead Settings. For other creation paths it is typically the user who created the record. A last entry with broad criteria that routes to a triage queue makes this visible instead of silent.
Queue assignment fails
A queue can only own records of the objects it supports. If you cannot choose a queue as the target of an entry, open the queue and confirm Lead is in its supported objects list. Also confirm the queue has members if you expect anyone to work the leads.
Reassignment surprises
Rule entries have a Do Not Reassign Owner option. If it is set and the lead already has a user owner, a rerun of the rule leaves the owner unchanged. Re-saving with the rule checked will not move such leads, which can look like the rule is broken.
Troubleshooting conversion
The Convert button is missing or the conversion is refused
Work through this checklist:
- The user needs the Convert Leads permission, plus create access to Account and Contact. Creating an opportunity also needs create access to Opportunity.
- The user needs edit access to the lead. Sharing and ownership apply.
- The lead should be owned by a user. A lead still owned by a queue typically has to be taken by a person before it converts.
- A lead that is already converted cannot be converted again. Query
IsConvertedto confirm. - The page layout or Lightning page must expose the Convert action.
Status errors
Conversion needs a Lead Status value that is marked as converted. If the converted status you pass is not one of those values, conversion fails. Add or fix the value under Lead Status picklist settings, and query it instead of hard-coding it:
SELECT MasterLabel FROM LeadStatus WHERE IsConverted = trueDo not delete a converted status value that existing converted leads use without replacing it first.
Validation and required fields
Conversion creates real Account, Contact, and Opportunity records, so validation rules and required fields on those objects apply. A custom field that is required on Contact but has no mapped lead field will block conversion. The fix is to map a lead field to it, give it a default value, or revise the rule so it allows records created by conversion. The Lead Settings option Require Validation for Converted Leads controls whether validation rules and required fields on the lead itself also run. Turn it on only if you want those lead rules enforced at conversion time.
Missing or unexpected data
- Only custom lead fields can be mapped to custom fields on the target objects. Standard fields follow fixed mappings.
- Mapped fields must have compatible data types. A text field mapped to a number field will not carry values you cannot parse.
- Mapping changes affect future conversions only. Records converted earlier are not updated.
- The matching list shown during conversion is how you attach the lead to an existing account or contact. If the user accepts the default "create new" choice, you get a duplicate.
- Duplicate rules on Account and Contact can alert or block when conversion creates records. Test your block rules with conversion in mind. A block rule that is fine for manual entry can stop conversions.
Troubleshooting Apex
Bulk conversion with partial success
Database.convertLead accepts a list, with a limit of 100 LeadConvert objects per call, and counts toward DML limits. Query the converted status once, outside any loop. Pass false for allOrNone so one bad lead does not roll back the rest, and read each result's errors.
LeadStatus convertStatus = [
SELECT MasterLabel
FROM LeadStatus
WHERE IsConverted = true
LIMIT 1
];
List<Lead> leads = [
SELECT Id, Email
FROM Lead
WHERE IsConverted = false
AND Status = 'Qualified'
AND Owner.Type = 'User'
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(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(true);
converts.add(lc);
}
List<Database.LeadConvertResult> results = Database.convertLead(converts, false);
for (Integer i = 0; i < results.size(); i++) {
if (results[i].isSuccess()) {
System.debug('Converted ' + leads[i].Id + ' to account ' + results[i].getAccountId());
} else {
for (Database.Error err : results[i].getErrors()) {
System.debug('Lead ' + leads[i].Id + ' failed: ' + err.getStatusCode() + ' - ' + err.getMessage());
}
}
}The 'Qualified' status is an example. Use a value that exists in your org. To attach to existing records, call setAccountId and setContactId on the LeadConvert.
Common Apex pitfalls
- A trigger that calls
convertLeadand also fires on Account or Contact inserts can recurse or hit limits. Guard your trigger logic. - Test classes need a converted
LeadStatusvalue.LeadStatusrows are setup data that tests can read withoutSeeAllData, but confirm the org has one. - Assignment rule options affect only the DML they are attached to. They do not change the org's default behavior.
Best practices
- Keep one active assignment rule with a small number of entries, ordered from most specific to most general, and end with a catch-all entry.
- Route unowned leads to a queue so they stay visible instead of landing on a default user.
- Make assignment explicit in every creation path: the layout checkbox, the import option, and
DMLOptionsin Apex. - Review field mappings whenever you add a custom field to Lead, Account, Contact, or Opportunity.
- Use a few converted status values with clear meaning, and keep other statuses short and ordered by the sales process.
- Query status and queue IDs by name or flag instead of hard-coding record IDs.
- Test rule and conversion changes in a sandbox with a sample lead from each source (UI, import, Web-to-Lead, Apex).
- Report on converted leads with the converted account, contact, and opportunity IDs to spot patterns, such as many conversions with no opportunity.
Exercise
A Web-to-Lead form creates leads that land with the default lead owner, though an assignment rule entry should match "Industry equals Healthcare". Name three things to check.
Show answer
First, that the rule is the active one, since only one lead rule can be active. Second, that the entry order does not let an earlier, broader entry match first. Third, that the submitted Industry value exactly matches the picklist value used in the entry criteria, because the form may be sending a different string.
Exercise
A nightly Apex job converts leads, and it fails for every lead with a status error. The job uses setConvertedStatus('Qualified'). What is wrong, and what is a more reliable way to write it?
Show answer
"Qualified" is not a Lead Status value marked as converted. Query LeadStatus where IsConverted = true, use its MasterLabel, and do the query once before the loop.