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

Apex trigger for revenue schedule??



I am fairly new to wirte an APEX code for this query. Hope someone will share code or comments.
if we create a booking & opportunity and add opportunity lie item, enable scheduling and define revenue schedule date with monthly installment.

Currently, user are keep changing revenue schedule date in a past or future which get impact on monthly revenues dashboard.


Now, we would like to restric user to changing revenue schedule date in past. how???

Thanks for help!


I will stuck to find a solution for my query based on above documents. I need to get the solution soon. So can you please post an example code for opportunitly lineitemschedule.



Shashikant SharmaShashikant Sharma

I hope this might help you



trigger setMonthlyAmountAndDuration on OpportunityLineItem (after insert, after update) {
    if(!SetMonthlyAmountHelper.hasAlreadySetMonthlyAmount()) {
        for (OpportunityLineItem LI : {
            PricebookEntry pbentry = [select Product2Id from PricebookEntry where Id = :LI.PriceBookEntryId];
            Product2 prod = [select CanUseRevenueSchedule, NumberOfRevenueInstallments from Product2 where Id = :pbentry.Product2Id];

            if (prod.CanUseRevenueSchedule==true && prod.NumberofRevenueInstallments>0) {
                OpportunityLineItem opp_prod = [select Monthly_amount__c, Contract_duration__c from OpportunityLineItem where Id = :LI.Id];
                List<OpportunityLineItemSchedule> schedule = [select ScheduleDate from OpportunityLineItemSchedule where OpportunityLineItemId = :LI.Id];

                if (schedule.size()==0) 
                	opp_prod.Monthly_amount__c = LI.UnitPrice;
                	opp_prod.Monthly_amount__c = LI.UnitPrice / schedule.size();
                opp_prod.Contract_duration__c = schedule.size();
                Update opp_prod;






Hi Shashikant,

Thanks for your reply on this. It was superfast to write an APEX trigger!

But I am not sure whether I need all the code to achieve my query. Or maybe I explained different.

I have attached ruff data.

Date                      Revenue               comments

02/02/2011         6000.00                 user will not allow to change schedule date in past.

25/05/2011         6000.00                 same as above

25/06/2011         6000.00                 user will allow to change in future as schedule date is in            

                                                              current month


Is you code will work? Or it will be different approach.

Hope I have explained correctly.  Look forward to see the replay.


Rahul S.ax961Rahul S.ax961

Hi Shashikant,


One Advice - Avoid query in loop, use Map instead.


Shashikant SharmaShashikant Sharma

Thanks for update,


I know it, but missed it :)


Wrote a blog for this :

But missed this time. :) 

Rahul S.ax961Rahul S.ax961

:P it happens...

btw nice blog..

Shashikant SharmaShashikant Sharma

Your welcome mate :) , Now onward I have to be little more careful before posting any thing.