+ Start a Discussion
Big EarsBig Ears 

OpportunityLineItemSchedules - DML Limits on complicated Payment Schedules

Dear all,


I'm trying to automate processes around Payment Schedules in Salesforce. I've created an APEX class that, when a deal is won, cycles through the OpportunityLineItems and uses provided data around Payment Terms to automatically create the schedule.


I've managed to get the SOQL queries down through use of maps, lists, etc, but still end up coming up against dml governer limits if more than 100 payment schedules end up being created (i.e. 9/10 items being paid monthly over the following year will mean that over 100 schedules have to be inserted).


I'm not sure if it can be batched, as only one record is being passed into the code at the beginning (the one opportunity being marked as closed-won), so am not sure if it's possible to break it down any further. Does anybody know how to deal with complicated payment structures like this?


My code, if it is useful, is as follows:


for(OpportunityLineItem o :OppItems){ Double TotalRevenue = OpportunityProductRevenues.get(o.id); Double NumberOfPayments = RefOpp.Payment_Terms_Number_of_Payments__c; Double RevenuePerPayment = TotalRevenue / NumberOfPayments; integer MonthsBetweenPayments = null; if(RefOpp.Payment_Terms__c == 'Annual'){ MonthsBetweenPayments = 12; } else if (RefOpp.Payment_Terms__c == 'Semi-Annually'){ MonthsBetweenPayments = 6; } else if (RefOpp.Payment_Terms__c == 'Quarterly'){ MonthsBetweenPayments = 3; } else if (RefOpp.Payment_Terms__c == 'Monthly'){ MonthsBetweenPayments = 1; } integer i = null; for(i=1 ; i <= NumberOfPayments ; i++){ Date iScheduleDate = RefOpp.closedate.addmonths(MonthsBetweenPayments*(i-1)); Double iScheduleQuantity = 0; if (i==1){ iScheduleQuantity = OpportunityProductQuantity.get(o.id); } OpportunityLineItemSchedule Schedule = new OpportunityLineItemSchedule(Type = 'Both', Quantity = iScheduleQuantity, OpportunityLineItemid = o.id, Revenue = RevenuePerPayment, ScheduleDate = iScheduleDate); Schedules.add(Schedule); } OpportunityLineItemSchedule TestSchedule = Schedules.get((j-1)*NumberofPayments.intValue()); Double ComparisonAmount = (TestSchedule.Revenue)*NumberofPayments; if(ComparisonAmount != OpportunityProductRevenues.get(o.id)){ Double Difference = ComparisonAmount - OpportunityProductRevenues.get(o.id); TestSchedule.revenue = TestSchedule.revenue - Difference; } j++; } insert Schedules; }



Any help would be greatly appreciated

Message Edited by Big Ears on 03-03-2009 10:25 AM

Just stumbled across your question.


I actually don't know how to solve the issue directly, however whenever I run into hard governor limits which can't be handled by 'standard' optimizations, I think about using a @future method in a utility class.


Of course this has the drawback of being asynchronous, so you would not see the schedule at once, but (usually) after a few seconds, the schedule should appear in your opportunity.