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
SvanskeySvanskey 

Duplicate Id in list Batch Apex

Hi Folks, 

I do receive "Duplicate Id in List" error when I run the code. Please could you revew and advise what is wrong and what can be improved here ?  Duplicate Id was found in  list "acctaddresstoupdate ".   Many Thanks in advance. 

global Database.QueryLocator start(Database.BatchableContext BC) {
       return Database.getQueryLocator([Select id, BDF_Target__c, OCE__PreferredAddress__c, OCE__PreferredAddress__r.BDF_Visited__c from OCE__AccountTerritoryFields__c where OCE__PreferredAddress__c != null]);
                         
    }

    global void execute(Database.BatchableContext BC, List<OCE__AccountTerritoryFields__c> scope) { system.debug(scope);
     
        list<OCE__AccountAddress__c> acctaddresstoupdate = new  list<OCE__AccountAddress__c> ();
        map<id,OCE__AccountAddress__c> Acctaddnodupes = new map <id,OCE__AccountAddress__c>();
       
            for(OCE__AccountTerritoryFields__c acctterfields : [Select id, BDF_Target__c, OCE__PreferredAddress__c, OCE__PreferredAddress__r.BDF_Visited__c from OCE__AccountTerritoryFields__c where id IN :scope]) {
                system.debug(acctterfields); 
                 acctaddresstoupdate.add(acctterfields.OCE__PreferredAddress__r); 
 
                if(acctterfields.BDF_Target__c == 'Yes') {
                    system.debug('Completed');
                    acctterfields.OCE__PreferredAddress__r.BDF_Visited__c = true;
                    update acctaddresstoupdate;
                    system.debug(acctaddresstoupdate);

                } 
                  else {
                     system.debug('Not Completed');
                     acctterfields.OCE__PreferredAddress__r.BDF_Visited__c = false;
                     update acctaddresstoupdate;
                     system.debug(acctaddresstoupdate);
                     
                } 
     
                }
                map<id,OCE__AccountAddress__c> AcctAddmap = new map <id,OCE__AccountAddress__c>();
                AcctAddmap.putAll(acctaddresstoupdate); 
                if(AcctAddmap.size()>0){
                update AcctAddmap.values();
              
                                    }

    }

AnudeepAnudeep (Salesforce Developers) 
Lists can hold duplicate values, but if it contains duplicate sObject IDs and you try to update or delete you'll get the error : 'System.ListException: Duplicate id in list'

Resolution:
  • Create a map of <id,sobject>
  • Convert the list to Map so that the duplicate IDs are removed.
  • Update or Delete the values part of the Map.
Sample Code
 
// pick up any id from your salesforce org, in this sample it is account id. 
id aid = '0017F000002WkkdQAC';

list <account> al = new list <account>();

for(account a : [select id from account where id ='0017F000002WkkdQAC']){
	account acc = new account(id = aid);
    al.add(a);
    al.add(acc);
}
//create a map that will hold the values of the list 
map<id,account> accmap = new map<id,account>();

//put all the values from the list to map. 
accmap.putall(al);
if(accmap.size()>0){
update accmap.values();
}

See Error 'System.ListException: Duplicate id in list' in Apex to learn more

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Andrew GAndrew G
convert the List you are updating to a Set.  By using a Set, SF will remove the duplicates for you.
Set<OCE__AccountAddress__c> acctaddresstoupdate = new  Set<OCE__AccountAddress__c> ();


You can then do the insert / update as required.


regards
Andrew