Loading

Error 'System.ListException: Duplicate id in list' in Apex

게시 일자: Oct 13, 2022
상세 설명

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'

솔루션
  1. Create a map of <id,sobject>
  2. Convert the list to Map so that the duplicate IDs are removed.
  3. 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();
}
List Class
Map Class
Disclaimer: This is one way of resolving the error and it is not the only way. Please use your discretion and test it in sandbox before deploying to production. 






 
Knowledge 기사 번호

000382864

 
로드 중
Salesforce Help | Article