0

I need to run though records and create child record based on the number of duration.

I encounter

System.LimitException: Too many DML statements: 151 error.

Please let me know what I should do to avoid this error

   List < Membership_Contract__c > mcUpdateForecast = new List < Membership_Contract__c > ();
   mcUpdateForecast = [SELECT Id, Expiry_Date__c, Amount_of_Program__c, Start_Date__c, Duration_in_Months__c FROM Membership_Contract__c WHERE Expiry_Date__c < > NULL AND Start_Date__c < > NULL AND Duration_in_Months__c < > NULL AND Amount_of_Program__c < > NULL];

   for (Membership_Contract__c cont: mcUpdateForecast) {
       Double duration = cont.Duration_in_Months__c;
       createNewForecast(cont, duration);
   }

   Private static void createNewForecast(Membership_Contract__c newF, Double duration) {
       List < Opportunity_Schedule__c > insertNewSched = new List < Opportunity_Schedule__c > ();

       For(Integer i = 0; i < duration; i++) {
           Opportunity_Schedule__c newOF = new Opportunity_Schedule__c();
           newOF.Membership_Contract__c = newF.Id;
           newOF.Forecast_Amount__c = newF.Amount_of_Program__c / duration;
           newOF.Month_number_sort__c = i;
           newOF.Forecast_Date__c = newF.Start_Date__c.addMonths(i);
           insertNewSched.add(newOF);
       }
       if (insertNewSched.size() > 0) {
           insert insertNewSched;
       }
   }
2
  • 2
    Your DML statement is inside for loop. Your code must be able to handle bulk records.
    – Mr.Frodo
    Nov 7, 2018 at 3:31
  • You should complete some basic Apex Trailhead modules including this one on bulk processing. Nov 8, 2018 at 1:24

1 Answer 1

0

You have a DML in a for loop:

for(Membership_Contract__c cont: mcUpdateForecast )
  ... createNewForecast(cont,duration);
    ... insert insertNewSched;

You need to create all the records in memory first, then then perform the DML:

List<Membership_Contract__c> mcUpdateForecast = 
    [SELECT Id, Expiry_Date__c, Amount_of_Program__c, Start_Date__c, Duration_in_Months__c FROM Membership_Contract__c WHERE Expiry_Date__c <> NULL AND Start_Date__c <> NULL AND Duration_in_Months__c <> NULL AND Amount_of_Program__c <> NULL];
List<Opportunity_Schedule__c> newOppSchedules = new List<Opportunity_Schedule__c>();


for(Membership_Contract__c cont: mcUpdateForecast ){
    newOppSchedules.addAll(createNewForecast(cont,cont.Duration_in_Months__c));        
}
insert newOppSchedules;

Private static Opportunity_Schedule__c[] createNewForecast(Membership_Contract__c newF, Double duration){
    List<Opportunity_Schedule__c> results = new List<Opportunity_Schedule__c>();

    For(Integer i = 0; i < duration; i++){
      Opportunity_Schedule__c newOF = new Opportunity_Schedule__c();
        newOF.Membership_Contract__c = newF.Id;
        newOF.Forecast_Amount__c = newF.Amount_of_Program__c/duration;
        newOF.Month_number_sort__c = i;
        newOF.Forecast_Date__c = newF.Start_Date__c.addMonths(i);
        results.add(newOF);
    }
    return results;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .