function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Samantha CataniaSamantha Catania 

DUPLICATE_DETECTED Error when converting lead from APEX but not screen

When trying to convert a Lead using the standard APEX method we get a DUPLICATE_DETECTED error because there is another Lead with the same email address. The same behaviour is not experianced with the exact same Lead when converting using the convert button on the screen.

Error Message:
System.DmlException: ConvertLead failed. First exception on row 0; first error: DUPLICATES_DETECTED, You're creating a duplicate contact. We recommend you use an existing record instead.: []

Apex Code (from SF Example in Doc (https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_dml_convertLead.htm))
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId('00Q5t000003eBKzEAM');
lc.setConvertedStatus('Closed - Converted');
Database.LeadConvertResult lcr = Database.convertLead(lc);

Is this a bug in the out of the box method or am I missing something?

 
Julien SalensonJulien Salenson
Hi Samantha,

I think, the reason why you might not encounter this issue when converting the Lead using the standard Convert button on the screen is that Salesforce has built-in duplicate detection logic in the standard conversion process.
When you manually convert a Lead through the Salesforce UI, it performs additional checks and gives you options to merge or link the Lead with an existing Contact or Account if it finds potential duplicates.

However, when you are converting Leads programmatically using Apex code, you need to handle duplicate detection and resolution yourself. This means you should check for existing Contacts or Accounts with the same email address before attempting to convert the Lead.

Here's a modified version of your code that includes a check for duplicates and handles them :
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId('00Q5t000003eBKzEAM');
lc.setConvertedStatus('Closed - Converted');

// Check for existing Contact with the same email address
List<Contact> matchingContacts = [SELECT Id FROM Contact WHERE Email = :lc.getLeadId()];

if (matchingContacts.isEmpty()) {
    // No matching Contact found, proceed with conversion
    Database.LeadConvertResult lcr = Database.convertLead(lc);
} else {
    // A matching Contact was found, handle the duplicate scenario here
    // You can choose to merge, link, or take any other appropriate action
    // For example, you can update the Lead to link it to the existing Contact
    // and then mark the Lead as converted
}

Or if you have a dupliacate rules, you should try something like :
Database.DMLOptions dmlOpts = new Database.DMLOptions();
    dmlOpts.DuplicateRuleHeader.DuplicateRuleId = 'My_Duplicate_Rule_API_Name';

    // Set the DMLOptions to use the specified Duplicate Rule for Contacts
    lc.setDMLOptions(dmlOpts);

    // Perform the Lead conversion
    Database.LeadConvertResult lcr = Database.convertLead(lc);
=> Specify the Duplicate Rule for Contacts (replace 'My_Duplicate_Rule_API_Name' with your rule's API name)

I hope this comment has helped you,
Julien