You need to sign in to do that
Don't have an account?
Need help "bulkifying" my apex trigger
Hello,
I have a trigger that sets the revenue schedule for opportunity line items. It works fine for updaitng line items one at a time. But I have a few hundred opportunites that I have to migrate from and old SF org and when I try to add the products to these opportunites via the data loader I get an error that it is exceeding the amount of scheudles the trigger can perform at once. I want to avoid doing this one by one and have been told that I just need to "bulkify" my trigger. I am new apex so can someone help me understand how I would do that? Is this a simple change? And would it affect how the trigger fires when my seller's are adding new line items or updating exisitng ones for indivual opportunites?
The trigger is included below:
trigger setRevenueSchedule on OpportunityLineItem (after insert, after update) {
if(RecursionBlocker.flag){
if (trigger.isAfter && trigger.isInsert){
RevenueController.insertRevenue(trigger.new);
}
if (trigger.isAfter && trigger.isUpdate){
RevenueController.updateRevenue(trigger.newMap, trigger.oldMap);
}
}//end of if recursionblocker is true.
}
Thanks in advance for any help!
Theo T
Please post yout RevenueController class code.
You can create trigger frame work according to below blog
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html
Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
{
public static void insertRevenue(List<OpportunityLineItem> LI){
List<OpportunityLineItemSchedule> oppLS = new List<OpportunityLineItemSchedule>();
Date ServiceDate;
Date EndDate;
Decimal monthlyRevenue = 0, TotalPrice = 0, UnitPrice = 0;
String RevenueInstallmentPeriod = 'Daily';
String RevenueScheduleType = 'Divide';
List<Id> oliIds = new List<Id>();
for (OpportunityLineItem olit : LI){
oliIds.add(olit.Id);
}
for (OpportunityLineItem item : [SELECT Id, ServiceDate, End_Date__c, UnitPrice, TotalPrice, PricebookEntryId FROM OpportunityLineItem Theo WHERE opportunity.Operative_Eligible__c!='Yes' and Theo.Id IN :oliIds]) {
ServiceDate = item.ServiceDate;
EndDate = item.End_Date__c;
TotalPrice = item.TotalPrice;
UnitPrice = item.UnitPrice;
NumberOfRevenueInstallments = (((EndDate.year() * 12) + EndDate.MONTH()) - ((ServiceDate.year() * 12) + ServiceDate.month()));
numberOfDays = ServiceDate.daysBetween(EndDate) + 1;
monthlyRevenue = (TotalPrice / numberOfDays);