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
David Cook 5David Cook 5 

ConverLlead failed. First exception on row 3; first error: DUPLICATE_VALUE, duplicate value found: []

I'm using skyvia to update fields on Leads in job that runs once a day. That job locates the lead to update using a custom unique field that corresponds to the lead's ID in our external database. I also have an apex trigger called AutoConvertLeads that runs after updates that converts leads if they have MRR_c > 0.

/* 
After a lead is updated or inserted, it adds the lead to a list if that lead has started paying.
Then, we call convertLead on that list, converting all qualified leads. 
*/
trigger AutoConvertLeads on Lead (after update, after insert) {

    Database.LeadConvert[] leadsToConvert = new List<Database.LeadConvert>();
	LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE Id='01J1a00000DO8hfEAD' LIMIT 1];
    
    for(Lead l: Trigger.new) { //iterate through array of leads
        if(!l.isConverted && l.MRR__c > 0) {
			Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(l.Id);
            lc.setDoNotCreateOpportunity(false);
            lc.setOpportunityName(l.Company + ' - self serve');
            lc.setConvertedStatus(convertStatus.MasterLabel);
            leadsToConvert.add(lc);
        }
    }
    
    if (!leadsToConvert.isEmpty()) {
	    List<Database.LeadConvertResult> lcr = Database.convertLead(leadsToConvert);
    }
    
    // This for loop updates the opportunities that were created above with more information
    for (Database.LeadConvert l: leadsToConvert) {
        Lead lead = new Lead();
        Account acc = new Account();
        Opportunity opp = new Opportunity();
        lead = [SELECT ConvertedAccountId, LeadSource, MRR__c FROM Lead WHERE Id = :l.getLeadId()][0];
        if (lead != null) {
            acc = [SELECT Id FROM Account WHERE Id = :lead.ConvertedAccountId][0];
            if (acc != null) {
		        opp = [SELECT Id FROM Opportunity WHERE AccountId = :acc.Id and Name like '% - self serve'][0];
		        opp.CloseDate = date.today();
		        opp.LeadSource = lead.LeadSource;
		        opp.Amount = lead.MRR__c;
		        opp.StageName = 'Closed Won';
		        opp.Type = 'New Business';
                opp.Renewal_Date__c = date.today().addMonths(1);
                opp.OwnerId = '0051a000002d9h7AAA';
		        update opp;
            }
        }
    }
}


This system was working just fine, but now it seems to be broken because most Leads that skyvia tries to update end up failing with the following error:

AutoConvertLeads: execution of AfterUpdate

caused by: System.DmlException: ConvertLead failed. First exception on row 3; first error: DUPLICATE_VALUE, duplicate value found: []

()
Some of the errors say "First exception on row 0"  instead of "row 3" but I can't figure out why. One of the thing I changed around the time when this stopped working was the duplicate roles in the salesforce instance, however, the message above is not the Alert Text for any of the duplicate rules.

The above error appears for both leads that have MRR_c > 0 as well as leads that don't, which doesn't make any sense because the trigger should only be calling convertLead on leads that have MRR_c > 0. Another thing that doesn't make any sense is there aren't any duplicates. I've searched our Salesforce and can't find any objects that have the same IDs as the leads that this error occurs with.

I would really appreciate it if someone could help me with both of these unexpected behaviors.
ForceWaveForceWave

Check the Lead Conversion mapping once. Seems like you have some unique field on Account or Contact which is having duplicate values. Check how the Unique Field mapped to Account or Contact in Lead Conversion Mapping. 

 
David Cook 5David Cook 5
Thanks for the reply! I checked, and that doesn't seem like the issue. There's only one field that maps from a lead to an account or contact that is used to detect duplicates and when I search salesforce for the value of that field from the lead that I'm trying to convert, salesforce doesn't return any accounts or contacts. In addition, if I convert the lead using the convert button inside the salesforce UI, the conversion completes without an error.

I saw somewhere that I might need to use a future method because the Skyvia update hasn't finished saving the records yet. Could that be the issue?

Also, I still don't know why leads that have an MRR_c of 0 are being added to the leadsToConvert list.
David Cook 5David Cook 5
I'm still stuck on this. Does anyone else have any ideas?