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
blackchirublackchiru 

System.ListException: Duplicate id in list

if(s.Approval_Status__c=='Order Canceled' && Trigger.oldMap.get(s.Id).Approval_Status__c == 'Approved by First Level'){
          for(Sample_Line_Item__c sli:s.Sample_Line_Items__r){
                    productIds.add(sli.product_No__c);
                    
                    if(s.Distribution_Center__c=='Tokyo')
                    {
                        if(TokyoproductIdToQuantity.get(sli.product_No__c)  == null || TokyoproductIdToQuantity.get(sli.product_No__c) == 0)
                        {   
                        
                            TokyoproductIdToQuantity.put(sli.product_No__c,sli.quantity__c * (-1));
                        }
                        else
                        {
                            Decimal temp = TokyoproductIdToQuantity.get(sli.product_No__c);
                            temp = temp + (sli.quantity__c * (-1));
                            TokyoproductIdToQuantity.put(sli.product_No__c,temp);
                        }
                    }
                
                    if(s.Distribution_Center__c=='Suzuyo')
                    {
                        if(SuzoyoproductIdToQuantity.get(sli.product_No__c)  == null || SuzoyoproductIdToQuantity.get(sli.product_No__c) == 0)
                        {   
                        
                            SuzoyoproductIdToQuantity.put(sli.product_No__c,sli.quantity__c * (0));
                        }
                        else
                        {
                            Decimal temp = SuzoyoproductIdToQuantity.get(sli.product_No__c);
                            temp = temp + (sli.quantity__c * (0));
                            SuzoyoproductIdToQuantity.put(sli.product_No__c,temp);
                        }
                    }
                    if(s.Distribution_Center__c=='Osaka')
                    {
                        if(OsakaproductIdToQuantity.get(sli.product_No__c)  == null || OsakaproductIdToQuantity.get(sli.product_No__c) == 0)
                        {   
                        
                            OsakaproductIdToQuantity.put(sli.product_No__c,sli.quantity__c * (0)) ;
                        }
                        else
                        {
                            Decimal temp = OsakaproductIdToQuantity.get(sli.product_No__c);
                            temp = temp + (sli.quantity__c * (0));
                            OsakaproductIdToQuantity.put(sli.product_No__c,temp);
                        }
                        
                    }   
                
                    sli.Prior_Approved_quantity__c = sli.Quantity__c;
                    sli.Quantity__c = 0;
                    sampleLineItemsToUpdate.add(sli);
                
                }
              
        }      

Ritesh AswaneyRitesh Aswaney

You're adding more than once instance of a record to a List, which is being used in an update.

 

A way around it could be to use a map, check if the key alreadys exists in the map, if so, then the put updates the existing entry

and then you could update mapName.values()

Shashikant SharmaShashikant Sharma

Hi,

 

You have added a record more than once in the list that you are updating ( I think you ar updating "sampleLineItemsToUpdate" list.

 

Create a Set of id's of that record that needed to updated.

Set<ID> sampleLineItemsToUpdateIds = new Set<ID>();

 

And add this in your code

//add method in set returns true if item not in the set and false if item already in the set

if(sampleLineItemsToUpdateIds.add())

{

  sampleLineItemsToUpdate.add(sli);

}

 

By this way you will be able to create a list of items with unique ids only.Duplicate ids will not be able to added. By this solution you will be updating record's instance when it comes for the first time.

 

 

Ankit AroraAnkit Arora

Either use Set instead of List to update or just go to this link. Sure it will help you.

 

http://boards.developerforce.com/t5/Apex-Code-Development/System-ListException-Duplicate-id-in-list-Help-in-Trigger-Please/td-p/181850

 

 

Thanks
Ankit Arora

 

JackOdellJackOdell

Hey guys.

 

When I turn my list into a Set I get the following compilation error: "DML requires SObject or SObject list type".

 

Will I need to turn the Set into a List before executing the update on it?  Or am I missing something?

 

 

Nitesh Gadkari 1Nitesh Gadkari 1
Hi All,

Please refer to this Knowledge article from Salesforce, which suggest one way to resolve the exception:
https://help.salesforce.com/articleView?id=000257270&language=en_US&type=1

Regards
Nitesh