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
SImmySImmy 

Please help me with Database.Saveresult iteration

Database.saveResult[] res= Database.insert(invoiceList,false);
          Integer result2=res.size();
          System.debug('inserted invoices for SA=BA'+res);
          System.debug('result size for errorlogs loop '+result2);
          if(res.size()>0)
          {
               System.debug('debugging inside inv res ');
               for(Integer i=0; i< res.size(); i++) 
               {
                           System.debug('inside result of invoices for errorlogging');
                          if(!res.get(i).isSuccess())
                           {
                             Database.Error errors =  res.get(i).getErrors().get(0);
                             Event_Tracker__c erlog=new Event_Tracker__c();
                             erlog.Error_Description__c='Invoice creation failed for SA=BA'+'\n'+String.valueof(res.get(i).getErrors().get(0))+'\n'+invoiceList[i];
                             erlog.name='Script 2 - 2 ST_DataTransformationBatch Script has error';
                             erlog.Error_Object__c=invoiceList[i].Opportunity__c;
                             erlog.recordtypeid=evtrecordtypeid;
                             errorlogsList.add(erlog); 
                             scriptfailedoptId.add(invoiceList[i].Opportunity__c);
                                                 
                           }
                            if(res.get(i).isSuccess())
                            {
                                optyVsInvoiceId.put(invoiceList[i].Opportunity__c,invoiceList[i].Id);
                            }
               }
           }

I am able to get the debug System.debug('debugging inside inv res ');
But unable to get the debug System.debug('inside result of invoices for errorlogging');
Can somebody please help me why this strange behaviour is coming as I need to insert the errorlogs records and because of this issue my error logs are not getting inserted. Please help me or suggest what should I do.
{tushar-sharma}{tushar-sharma}
What is your debug log size here. When we reached the limit Salesforce skip some debug logs.
David Zhu 🔥David Zhu 🔥
Sounds strange but sometimes happens.😉 I would try replacing for with foreach and check.

foreach (Database.saveResult result : res)
SImmySImmy
It worked.