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
Theodosia TeniaTheodosia Tenia 

Need help "bulkifying" my apex trigger


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 (trigger.isAfter && trigger.isInsert){
        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

Leslie  KismartoniLeslie Kismartoni
What does the code for RevenueController.updateRevenue() and .insertRevenue() look like?
Amit Chaudhary 8Amit Chaudhary 8
Hi Theodosia Tenia,

Please post yout RevenueController class code.
You can create trigger frame work according to below blog

Amit Chaudhary
Theodosia TeniaTheodosia Tenia
public class RevenueController
    public static void insertRevenue(List<OpportunityLineItem> LI){
        List<OpportunityLineItemSchedule> oppLS = new List<OpportunityLineItemSchedule>();
        Date ServiceDate;
        Date EndDate;

        Decimal monthlyRevenue = 0, TotalPrice = 0, UnitPrice = 0;
Theodosia TeniaTheodosia Tenia
  Integer NumberOfRevenueInstallments, numberOfDays;
        String RevenueInstallmentPeriod = 'Daily';
        String RevenueScheduleType = 'Divide';
        List<Id> oliIds = new List<Id>();
        for (OpportunityLineItem olit : LI){
        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);
Theodosia TeniaTheodosia Tenia
Sorry, this is only a piece of it.  I kept getting an error when i try to post the full code, but it seems to work when i paste it in pieces.  But not i am getting a new error when i am trying to post the rest. I have contacted the fourm admin for help.
Amit Chaudhary 8Amit Chaudhary 8
Please send me your full code on my mail id So that i review your code.