+ Start a Discussion

System.ListException: Duplicate id in list Error.

We're currently getting a System.ListException: Duplicate id in list error in one of our triggers, but can't figure out why.


Any ideas?



Here is our code:


trigger TrackingNumberTrigger on Tracking_Number__c (after insert, after update) {

List<Case> casesToUpdate = new List<Case>();
Set<Id> orderNumbers = new Set<Id>();

for(Tracking_Number__c trackingNum : trigger.new) {
if(trackingNum.Order__c!=null) {
if(!orderNumbers.contains(trackingNum.Order__c)) {

List<Case> caseList = new List<Case>([Select Id, Order_Number__c, Related_To_Order__c From Case Where Related_To_Order__c IN :orderNumbers LIMIT 1000]);

for(Tracking_Number__c trackingNum : trigger.new) {
for(Case a : caseList) {
if(a.Order_Number__c==null && a.Order_Number__c==trackingNum.Name) {
a.Related_To_Order__c = trackingNum.Name;
// currently assume 1 to 1 mapping
if(trackingNum!=null && trackingNum.Order__c==a.Related_To_Order__c) {
a.Tracking_Number__c = trackingNum.Name;
a.Shipper__c = trackingNum.Shipper__c;
a.Tracking_URL__c = trackingNum.Tracking_Url__c;
update casesToUpdate;





Your line casesToUpdate(a) will multiple times for each of those cases (one per tracking number) irrespective if there is a match between them because this line is not inside the if statements.


Having the same id twice or more in an update will give your error.