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
Sainath VenkatSainath Venkat 

Trigger throwing duplicate Id in list while updating

I have the code which is trying to update records but I am getting error that duplicate Id in list, below is my class
I am getting error at update clonedPeriods
 
List<Rent_Schedule_Period__c> periods = [
        SELECT Id, Rent_Schedule__c
        FROM Rent_Schedule_Period__c
        WHERE Rent_Schedule__c IN :scheduleIds
    ];
    if(periods.isEmpty()) {
        return;
    }
    Set<Id> periodIds = new Map<Id, Rent_Schedule_Period__c>(periods).keySet();
    Map<id,List<Rent_Schedule_Period__c>> clonedMap=new Map<id,List<Rent_Schedule_Period__c>>();
   // List<Id> periodIdList = new List<Id>(periodIds);
    List<Rent_Schedule_Period__c> clonedPeriods = new CloneService()
        .withSource(new List<Id>(periodIds))
        .deepClone();
   List<Rent_Schedule_Period__c> toUpdate=new List<Rent_Schedule_Period__c>();
    Set<Rent_Schedule_Period__c> setrecords=new Set<Rent_Schedule_Period__c>(clonedPeriods);
    List<Rent_Schedule_Period__c> uniqueRecords=new List<Rent_Schedule_Period__c>(setrecords);

   String periodId;
    if(rentScheduleId!=null){
     List<Rent_Schedule_Period__c> period=[select id,name from Rent_Schedule_Period__c where Rent_Schedule__c=:rentScheduleId];
        if( period.size()>0){
            for(Rent_Schedule_Period__c pro : period)
            periodId=pro.id;
        }
    }
    system.debug('clonedPeriods.size()=='+clonedPeriods.size());
    for (Integer i = 0; i < clonedPeriods.size(); i++) {
        Rent_Schedule_Period__c clonedPeriod = clonedPeriods.get(i);
        system.debug('periodId=='+periodId);
        system.debug('clonedPeriod=='+clonedPeriod);
        if(periodId!=null){
           clonedPeriod.id=periodId;
        }else{
           clonedPeriod.Rent_Schedule__c =clonedScheduleByOriginalId.get(clonedPeriod.Rent_Schedule__c);
        }
   toUpdate.add(clonedPeriod);
    }
    if(periodId!=null){
        system.debug('uniqueRecords==='+clonedPeriods);
        update clonedPeriods;
        //update RentScheduleService.getPriorPeriodUpdates(toUpdate);
    }else{
        insert clonedPeriods;
       // update RentScheduleService.getPriorPeriodUpdates(clonedPeriods);
    }
    update RentScheduleService.getPriorPeriodUpdates(clonedPeriods);

    RentScheduleService.evaluateAndSave(SObjectUtils.collectIds(clonedSchedules));
}

 
Suraj Tripathi 47Suraj Tripathi 47
Hi Sainath,
Greetings!

In Salesforce, List allows storing duplicate values.
But when you perform a DML on List when and List containing duplicate Id, then this error will occur.
for refer- https://salesforcescool.blogspot.com/2018/11/systemlistexception-duplicate-id-in.html

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi
PriyaPriya (Salesforce Developers) 
Hi Sainath,
 

Check below example that matches your requirement :- 

https://developer.salesforce.com/forums/?id=906F00000008z2GIAQ

https://salesforce.stackexchange.com/questions/200749/system-listexception-duplicate-id-in-list

http://learningthecloudway.blogspot.com/2015/07/how-to-deal-with-systemlistexception.html

Please mark it as the Best Answer so that it can help others in the future.

Regards,

Priya Ranjan