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
amritamrit 

System.List Exception: Duplicate id in list: How to avoid this??

Hi,

 

 

Here i got where im trying to update multiple records.But its showing me error like System.List Exception: Duplicate id in list: a07Q0000006oUdqIAE. How to avoid this.


rigger AutoSetoff on Receipt__c (after update,after insert) {

   List<Id> oid=new List<Id>();
   List<Id> rid=new List<Id>();
   List<Receipt__c> rec= new List<Receipt__c>();
  For (Receipt__c R: trigger.new)
    {
       rid.add(R.id);
       system.debug('Add receipt id'+rid);
       
       oid.add(R.Opportunity__c);
       system.debug('Add opp id'+oid);
       }
       rec=[select id,Opportunity__c,Realization_Status__c from Receipt__c where id=:rid];
       system.debug('Receipt'+rec);
       List<Opportunity> O = [Select Name,TotalReceipt_Realized__c,Total_Payment_Received__c from Opportunity
                         Where id =: oid];
       system.debug('OPP'+ O);                  
                         
       List<AggregateResult> groupedResults = [SELECT Sum(Amount__c)aver FROM Receipt__c
                                                Where Opportunity__c =: oid
                                               ];
       system.debug('Groupedresults query'+groupedResults );
       Double TotalReceipt = 0.0 ;
       for(Receipt__c rct:rec)
       {
       if(rct.Realization_Status__c =='Realized')
       {
        if(groupedResults.size() > 0)
        {
            system.debug('Size of grouped results'+groupedResults.size());
            String str = '' + groupedResults[0].get('aver') ;
            system.debug('Checking groupedResults'+str); 
            TotalReceipt = Double.ValueOf(str) ;
            System.debug('TotalReceipt ::::: ' + TotalReceipt) ;
        }
          for(Opportunity Op:O)
          {
            Double dif = TotalReceipt - Op.Total_Payment_Received__c;
            system.debug('Difference'+dif);
            List <Payment_Schedule__c> SList = new list <Payment_Schedule__c>([Select Name,TotalInstallment_Amount__c,Display_Order__c,  Amount_Due__c, Payment_Received__c from Payment_Schedule__c 
                 where Opportunity__c =: Op.id AND Payment_Schedule__c.Amount_Due__c > 0
                 Order By Display_Order__c]);
       
       
             For (Payment_Schedule__c S: SList)
               {
                system.debug('###Inside For');
                 
                 If (S.Amount_Due__c > dif)
                   {
                        system.debug('###Inside If 1'+ S.Payment_Received__c);
                  
                        S.Payment_Received__c = S.Payment_Received__c + dif;
                        system.debug('PaymentReceived'+S.Payment_Received__c );
                        dif = 0;
                    }
            If (S.Amount_Due__c < dif)
            {
             
                S.Payment_Received__c = S.Payment_Received__c + S.Amount_Due__c;
                system.debug('PaymentReceived ########'+S.Payment_Received__c );
                dif = dif - S.Amount_Due__c;
                   system.debug('Differenceone'+dif);
            }
        }
        Update SList;
    
}
}
}
      

 }

 

Force.comForce.com

For updating records, you need to have a List of sobject which contains unique values.

 

Possible workaround:

 

you can add all the elements of the list in a SET to remove duplicates eg:

 

Set<Payment_schedule> SSet = new Set<Payment_schedule>();

SSet.addall(Slist);              //removing duplicates

 

List<Payment_schedule> SListTobeUpdated = new List<Payment_schedule>();

SListTobeUpdated.addall(SSet);              //DML operations are not allowed on SET , so adding all elements in a LIst again.

upsert SListTobeUpdated;

 

SListTobeUpdated = null;

Slist = null;

SSet = null;


Thanks,

Pragati

amritamrit

Thanks for your reply.

 

I tried to use like this.

 for(Opportunity Op:O)
          {
            Double dif = TotalReceipt - Op.Total_Payment_Received__c;
            system.debug('Difference'+dif);
            List <Payment_Schedule__c> SList = new list <Payment_Schedule__c>([Select Name,TotalInstallment_Amount__c,Display_Order__c,  Amount_Due__c, Payment_Received__c from Payment_Schedule__c 
                 where Opportunity__c =: Op.id AND Payment_Schedule__c.Amount_Due__c > 0
                 Order By Display_Order__c]);
            Set<Payment_Schedule__c> SSet = new Set<Payment_Schedule__c>();
            SSet.addall(Slist);     
            List<Payment_Schedule__c> SListTobeUpdated = new List<Payment_Schedule__c>();
            SListTobeUpdated.addall(SSet);
            system.debug('Updated SList'+ SListTobeUpdated);  
           

 
             For (Payment_Schedule__c S: SList)
               {
                system.debug('###Inside For');
                 
                 If (S.Amount_Due__c > dif)
                   {
                        system.debug('###Inside If 1'+ S.Payment_Received__c);
                  
                        S.Payment_Received__c = S.Payment_Received__c + dif;
                        system.debug('PaymentReceived'+S.Payment_Received__c );
                        dif = 0;
                    }
            If (S.Amount_Due__c < dif)
            {
             
                S.Payment_Received__c = S.Payment_Received__c + S.Amount_Due__c;
                system.debug('PaymentReceived ########'+S.Payment_Received__c );
                dif = dif - S.Amount_Due__c;
                   system.debug('Differenceone'+dif);
            }
        }
        
        Update SList;
        upsert SListTobeUpdated;
        SListTobeUpdated = null;

          Slist = null;

           SSet = null;    

           
            
    
}

 But the list is showing null. Its showing null pointer exception.  I changed the code like this.

Force.comForce.com

Hi Amrit,

 

Try to replace your code with the below written code:

 

for(Opportunity Op:O)
          {
            Double dif = TotalReceipt - Op.Total_Payment_Received__c;
            system.debug('Difference'+dif);
            List <Payment_Schedule__c> SList = new list <Payment_Schedule__c>([Select Name,TotalInstallment_Amount__c,Display_Order__c,  Amount_Due__c, Payment_Received__c from Payment_Schedule__c 
                 where Opportunity__c =: Op.id AND Payment_Schedule__c.Amount_Due__c > 0
                 Order By Display_Order__c]);
if(SList.size() >0){
 Set<Payment_Schedule__c> SSet = new Set<Payment_Schedule__c>(); SSet.addall(Slist);

  List<Payment_Schedule__c> SListTobeUpdated = new List<Payment_Schedule__c>(); SListTobeUpdated.addall(SSet); system.debug('Updated SList'+ SListTobeUpdated); For (Payment_Schedule__c S: SListTobeUpdated) { system.debug('###Inside For'); If (S.Amount_Due__c > dif) { system.debug('###Inside If 1'+ S.Payment_Received__c); S.Payment_Received__c = S.Payment_Received__c + dif; system.debug('PaymentReceived'+S.Payment_Received__c ); dif = 0; } If (S.Amount_Due__c < dif) { S.Payment_Received__c = S.Payment_Received__c + S.Amount_Due__c; system.debug('PaymentReceived ########'+S.Payment_Received__c ); dif = dif - S.Amount_Due__c; system.debug('Differenceone'+dif); } } } //Update SList;
if(SListTobeUpdated != null || SListTobeUpdated.size()>0)
  upsert SListTobeUpdated;
SListTobeUpdated = null; Slist = null; SSet = null; }