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
LakshmanLakshman 

How to insert OpportunityLineItemSchedule automatically after insert of OpportunityLintItem

Hi All,

 

I want to automate OpportunityLineItemSchedule insert. For this I have written trigger which invokes future method as below:

 

 

global class OLISchedulerAuto{

    @future 
    public static void OLIScheduler(Set<Id> oliIds) 
    {
        List<OpportunityLineItem> o = new List<OpportunityLineItem>();
        List<OpportunityLineItemSchedule> Schedules = new List<OpportunityLineItemSchedule>();
        o = [Select Id, 
                    Duration__c, 
                    UnitPrice, 
                    ServiceDate, 
                    End_Date__c 
               From OpportunityLineItem where Id In :oliIds];
        for(OpportunityLineItem objO :o)
        {
            Double TotalRevenue = objO.UnitPrice;
            Double NumberOfPayments = objO.Duration__c;
            Double RevenuePerPayment = TotalRevenue / NumberOfPayments;
            Schedules = [Select Id from OpportunityLineItemSchedule where OpportunityLineItemId = :objO.Id];
            delete(Schedules);
            for(Integer i=1; i <= NumberOfPayments ; i++){
                                                            
            Date iScheduleDate = objO.ServiceDate.addMonths(objO.End_Date__c.monthsBetween(objO.ServiceDate)*(i-1));
            //RefOpp.closedate.addmonths(MonthsBetweenPayments*(i-1));
            
    
            OpportunityLineItemSchedule Schedule = new OpportunityLineItemSchedule(Type = 'Both', 
                                                                                   Quantity = 1, 
                                                                                   OpportunityLineItemId = objO.id, 
                                                                                   Revenue = RevenuePerPayment, 
                                                                                   ScheduleDate = iScheduleDate);
            Schedules.add(Schedule);
                                
            }
        }
        insert Schedules;                                                                              
    }
}

I think this will come into recursion as the Trigger is on OpportunityLineItem as below and after insert of schedules it will update OLI as well.

 

trigger TriggerOnOpportunityLineItem on OpportunityLineItem (after insert, after update) {
    Set<Id> oliIds = new Set<Id>();
    for(OpportunityLineItem oli: trigger.new)
    {
        oliIds.add(oli.Id);
    }
    if(oliIds.size()>0)
    {
       OLISchedulerAuto.OLIScheduler(oliIds);
    }
}

 

I just want to achieve automatic scheduling.

Please let me know your valuable comments on this. Thank you!

 

Regards,

Lakshman

 

Daniel BallingerDaniel Ballinger

I ran into the recursive trigger issue when inserting OpportunityLineItemSchedule records from an after insert trigger on OpportunityLineItem.

 

By first checking if a static boolean that gets set at the end of the first trigger invocation is true the recursion could be avoided.

 

There is an example here: Controlling Recursive Triggers