1

This trigger is used to calculate a running total of percent values for all the child records. Triggger.New is always on one record and it brings the correct value for the fiscal year. Based on this fiscal year the query allmai has records to be updated.Currently only 3 records. The calculation inside the for loop is working and getting to the debug line before update allmai.But then the update fails with

'Internal Salesforce.com error'

Looks like it is looping more than 3 times 22:20:13:000 LIMIT_USAGE_FOR_NS Number of SOQL queries: 19 out of 100

trigger Metrics_UpdateAIRunningTotal on Metrics_AI__c (after update){
    String CurrentFY;
    For(Metrics_AI__c mai : Trigger.New){
        //Get the value of the fiscal year for this record
        CurrentFY = mai.Fiscal_Year__c; 
        //Get all the related child records for this fiscal year
        List<Metrics_AI__c > allmai =   [Select MetricA_IParent_Id__r.id, 
          id, 
          Reporting_Month__c, 
          ADRC_Percent_of_allocated_Dol_expended__c,
          CopyADRC_Percent_of_allocated_Dol_expend__c,
          ADRC_Total_Dol_Expended__c,
          Fiscal_Year__c,
          Total_To_Date__c  
          from Metrics_AI__c 
          where MetricA_IParent_Id__r.fiscal_year__c = :CurrentFY
          order by Reporting_Month__c];    
          try{
          if (allmai.size()>0){
               for(Integer a = 0; a < (allmai.size()-1); a++){

                  //update the running total for the entire array
                  if(allmai[a+1].ADRC_Percent_of_allocated_Dol_expended__c != null || allmai[a+1].ADRC_Percent_of_allocated_Dol_expended__c != 0){
                      allmai[a+1].Total_To_Date__c = (allmai[a].Total_To_Date__c + allmai[a+1].ADRC_Percent_of_allocated_Dol_expended__c);
                      System.debug( a + '  ' + allmai[a].ADRC_Percent_of_allocated_Dol_expended__c + '   ' + allmai[a].Total_To_Date__c);
                   }

              }
              //System.debug('update will be here');
              //update allmai;
                            }

          }catch(Exception e){System.debug('@@@@@@@@ Exception:  ' + e + 'at line  ' + e.getLineNumber());}
    }
}
1
  • Do you have some form of recursion detection? If not the update will first the trigger again, rinse an repeat until error. And you need to get the queries outside of the for loop. Same for the update. Use collections (lists and maps) to gather the information before you process it
    – Eric
    Dec 29, 2015 at 6:51

0

You must log in to answer this question.

Browse other questions tagged .