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
sfuser12sfuser12 

System.ListException: Duplicate Id in List Error

Hello,

I am getting error duplicate Id. Can anyone suggest what to do?
   Set<Id> leadIds = new Map<Id, Lead>([SELECT Id, OwnerId 
                                FROM Lead]).keySet();
        
        List<Lead> leads = [SELECT Id, OwnerId 
                                FROM Lead 
                                Where LeadSource =: LeadSourceSelection
                                and Owner.Name =:LEAD_QUALIFICATION_QUEUE_NAME 
                                and Id=:leadIds
                           ];
        
        List<Lead> leadsToUpdate=new List<Lead>();
        
        for( UserWrapper agentUserWrapper : agentWrappers ) {            
            System.debug('CFRRetentionSiteDashboardController2.assignLeads - agent ' + agentUserWrapper.agent );
            System.debug('CFRRetentionSiteDashboardController2.assignLeads - agentUserWrapper ' + agentUserWrapper.numberOfLeadsToAssign );
          //  System.debug('CFRRetentionSiteDashboardController2.assignLeads - ListOfLead - ' + ListOfLead );

            for(Lead lead :leads) {
                    System.debug('CFRRetentionSiteDashboardController2.assignLeads - lead--' +lead);
                    lead.OwnerId = agentUserWrapper.agent.Id;
                    lead.Retention_Owner__c = agentUserWrapper.agent.Name;
                    if(currentSite != null) {
                        lead.Retention_Site__c = currentSite.Id;
                        leadsToUpdate.add(lead); 
                    }
              }                   
        }
        update leadsToUpdate;
        System.debug('CFRRetentionSiteDashboardController2.assignLeads-- leadsToUpdate'+leadsToUpdate); 
Ravi Dutt SharmaRavi Dutt Sharma
Why do you have a for loop inside another for loop. For the first iteration of agentWrappers list, it will add all the lelads to leadsToUpdate list. Then for the next iteration, it will again add the same leads to leadsToUpdate list.
Raj VakatiRaj Vakati
Try this code
 
Set<Id> leadIds = new Map<Id, Lead>([SELECT Id, OwnerId 
                                FROM Lead]).keySet();
        
        List<Lead> leads = [SELECT Id, OwnerId 
                                FROM Lead 
                                Where LeadSource =: LeadSourceSelection
                                and Owner.Name =:LEAD_QUALIFICATION_QUEUE_NAME 
                                and Id=:leadIds
                           ];
        
        Map<Id,Lead> leadsToUpdate=new Map<Id,Lead>();
        
        for( UserWrapper agentUserWrapper : agentWrappers ) {            
            for(Lead lead :leads) {
                    lead.OwnerId = agentUserWrapper.agent.Id;
                    lead.Retention_Owner__c = agentUserWrapper.agent.Name;
                    if(currentSite != null) {
                        lead.Retention_Site__c = currentSite.Id;
						if(!leadsToUpdate.contains(lead.id)){
                        leadsToUpdate.put(lead,Id , lead); 
						}
                    }
              }                   
        }
        update leadsToUpdate.values();
        System.debug('CFRRetentionSiteDashboardController2.assignLeads-- leadsToUpdate'+leadsToUpdate);