Does Database.convertLead() allow conversion of multiple leads to the same account at once? That seems like it should be no problem, but I'm getting strange errors in my unit testing.

I wrote a lead trigger to convert all new leads. In my unit test, I create two leads and put them in a list. The leads have different names and email addresses but the same Account ID. (This is a non-profit and they place all their contacts in a single "bucket" account.)

When I insert the lead list, the leads are converted, but I also find these errors in the debug log:

08:09:50.608 (20608468000)|FATAL_ERROR|System.ListException: Duplicate id in list: 001J000001EZwn6IAD

(System Code)

08:09:50.608 (20608490000)|FATAL_ERROR|System.ListException: Duplicate id in list: 001J000001EZwn6IAD

(System Code)

I don't understand why these fatal errors are occurring on the account ID and yet the leads are converting. I know there are no duplicate leads in the LeadConvert list because I'm using map.Values() in the for loop to populate the LeadConvert list, and also I have examined the LeadConvert list before it converts.

@James - Actually I found your post earlier but I didn't want to believe that convertLead() couldn't convert multiple leads with the same account. What's even more confusing is that the leads get converted despite the error. It doesn't make sense.

@Jason - Here's my trigger handler class. I'm passing Trigger.new and Trigger.newMap to it.

public class LeadAfterInsertHandler {

    public static void process(Lead[] newList, Map<Id, Lead> newMap) {
        String msg = '';
        Set<String> emailSet = new Set<String>();
        Map<String, Contact> conMap = new Map<String, Contact>();
        Contact[] consToUpdate = new List<Contact>();

        //New contacts are linked to an account named Individual (a bucket).
        //The ID is stored in Custom Settings.      
        Lead_Conversion_Settings__c settings = Lead_Conversion_Settings__c.getInstance();

        //Get email addresses.
        for(Lead l : newList) {

        //Get existing contacts.        
        for(Contact con : [select Email from Contact where email in :emailSet]) {
            conMap.put(con.Email, con);

        //Prep the leadConvert object.
        Database.LeadConvert[] lcList = new List<Database.LeadConvert>();
        for(Lead l : newList) {
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setConvertedStatus('Closed - Converted');
            //Contact Id if it already exists.
            if(conMap.keySet().contains(l.Email)) {

        if(!lcList.isEmpty()) {
            Database.LeadConvertResult[] lcrList = Database.convertLead(lcList, false);
            for(Database.LeadConvertResult lcr : lcrList) {
                //Capture failed conversions for the email message.
                if(!lcr.isSuccess()) {
                    msg = msg + newMap.get(lcr.getLeadId()).FirstName + ' ' + newMap.get(lcr.getLeadId()).LastName + ' conversion error: '
                     + lcr.getErrors()[0].getMessage() + '\n';

        //Send email with failed conversions.
        if(msg != '') {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] { settings.Error_Message_Recipient__c };
            mail.setSubject('Lead conversion errors');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });           





You must log in to answer this question.

Browse other questions tagged .