You need to sign in to do that
Don't have an account?
Why am I getting ListException: duplicate id in list errors?
I have this section of code as the bulk of a before insert/update apex trigger on my organization's Inspection_Checklist object:
trigger NewCapEx on Inspection_Checklist__c (before insert, before update) {
Inspection_Checklist__c[] checklists = Trigger.new;
//for the purpose of making this bulk-safe, get the prop evals in bulk now
//the next step is to refactor the two or so methods downstairs to take as an argument the prop eval, and since we have that propeval
//here, we just call it with it and blam we're good
Set<id> propEvalIDs = new Set<id>();
for (Inspection_Checklist__c checklist : checklists) {
propEvalIDs.add(checklist.Property_Evaluation__c);
}
Map<id,Property_Evaluation__c> assocPropEvals =
new Map<id,Property_Evaluation__c>([select id, Name, Inspector__c, Inspection_Date__c,
Inspection_Completed__c, Occupied_Status__c, Inspection_Est_Construction_Cost__c,
Active__c, PropChannelProxy__c from Property_Evaluation__c where id in :propEvalIDs]);
system.debug('Prop Eval map: ' + assocPropEvals);
Set<Property_Evaluation__c> toBeUpdated = new Set<Property_Evaluation__c>();
for (Inspection_Checklist__c checklist : checklists) {
decimal capex = CapExEstimate.GetCapExEstimate(checklist);
checklist.CapExNewEstimate__c = capex;
Property_Evaluation__c updated = CapExEstimate.UpdatePropertyEvals(checklist, assocPropEvals);
toBeUpdated.add(updated);
checklist = CapExEstimate.AdditionalChecklistChecks(checklist);
checklist = CapExEstimate.setPurchaseChannel(checklist, assocPropEvals);
}
//once out of the for loop update the set
//except you can't update a set for some reason so first convert it to a list
List<Property_Evaluation__c> updateanda = new List<Property_Evaluation__c>();
updateanda.addAll(toBeUpdated);
system.debug('What\'s in the list: ' + updateanda);
update updateanda;
}
When I run this trigger, on many but not all updates, I'm getting a ListException error on that last line, the "update updateanda;", saying that I have duplicate IDs in the list. However, I just created that list two lines up, and I populate it by calling addAll() on a Set - and a set, by definition, cannot have duplicates. So I have no idea how there could possibly be duplicate records in that list! Nonetheless I added in the call to system.debug, and lo and behold it was showing duplicate entries in that list, which explains why the code fails on the update line right below it. Does anyone have any insight on how I might go about solving this?
try with this
I have updated your code to allow unique ids in toBeUpdated List. do no duplicate ( same record) will enter more than once in this list.