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
SHKSHK 

How to fix the System Exception List: Duplicate Id at Line 396

Here's the class : http://pasted.co/b37739a9

Tell me how to fix the error and why is it happening?
v varaprasadv varaprasad
Hi Sree

 if(accToUpdate.size() > 0) {
                update accToUpdate;
                system.debug('>>>>>>'+accToUpdate);
                for(Account acc : accToUpdate) {
                    partnerHierAccountMap.put(acc.Partner_Hierarchy_Level__c, acc);
                }
            }

            if(accToInsert.size() > 0) {
                insert accToInsert;     
                for(Account acc : accToInsert) {  
                      if(!partnerHierAccountMap.containsKey(acc.id)
                    partnerHierAccountMap.put(acc.Partner_Hierarchy_Level__c, acc);  
                }
            }

Please check map is having allready id or not based on that it will not allow duplicates.
Please let me know in case of any assistance.


Thanks
Varaprasad
SHKSHK
The error is occuring at the line in BOLD. Please tell me how to fix it : 

 if(accToUpdate.size() > 0) {
                update accToUpdate;
                system.debug('>>>>>>'+accToUpdate);
                for(Account acc : accToUpdate) {
                    partnerHierAccountMap.put(acc.Partner_Hierarchy_Level__c, acc);
                }
            }

            if(accToInsert.size() > 0) {
                insert accToInsert;     
                for(Account acc : accToInsert) {  
                      if(!partnerHierAccountMap.containsKey(acc.id)
                    partnerHierAccountMap.put(acc.Partner_Hierarchy_Level__c, acc);  
                }
            }
 
v varaprasadv varaprasad
Hi 

First add inserted records ids
then add updated record ids like below.

  if(accToInsert.size() > 0) {
                insert accToInsert;     
                for(Account acc : accToInsert) {                 
                    partnerHierAccountMap.put(acc.Partner_Hierarchy_Level__c, acc);  
                }
            }

 if(accToUpdate.size() > 0) {
                update accToUpdate;
                system.debug('>>>>>>'+accToUpdate);
                for(Account acc : accToUpdate) {
                   if(!partnerHierAccountMap.containsKey(acc.id)
                    partnerHierAccountMap.put(acc.Partner_Hierarchy_Level__c, acc);
                }
            }
SHKSHK
Same Error Occurs.... How to remove duplicates from the list before updating?
v varaprasadv varaprasad
Hi,
use set instead of list it will not allow duplicates.


list<string> lstStrs = new list<string>();
lstStrs.add('ramu');
lstStrs.add('ramu');
lstStrs.add('ramu1');


set<string> setStrs = new set<string>();
setStrs.add('ramu');
setStrs.add('ramu');
setStrs.add('ramu1');

system.debug('===list==='+lstStrs.size());
system.debug('===set==='+setStrs.size());