I have a use case where I need to do an Aggregate SOQL on OpportunityLineItem(oli) & its opportunitylineitemschedules(olischedules).
I have 2 triggers(Trigger1 on Opportunity & 1 on oli). On Opportunity Trigger(trigger1), we have a use case where we Update Oli--> then delete olischedules --->and then insert olischedules with updated/lastest Oli values. (trigger 2) gets fired on oli update and runs a Aggregate SOQL -
[SELECT SUM(Revenue), CALENDAR_YEAR(ScheduleDate) month_year,
CALENDAR_MONTH(ScheduleDate) month_number
FROM OpportunityLineItemSchedule
WHERE OpportunityLineItemId IN: oliIds
GROUP BY CALENDAR_YEAR(ScheduleDate), CALENDAR_MONTH(ScheduleDate)]
The results here are returning the old olischedules which makes sense as the delete olischedules & insert olischedules are done after the Oli is updated & thats where this query is processed.
I need a way to delay the firing of trigger2 so that the delete olischedules and insert olischedules are from trigger1 are complete.
@future
may not work all the time as we cannot predict the execution time. Schedulable apex with batch apex are my next go tos to set a time to schedule only ONCE. Not sure how what the cron would be for this. I might be doing wild-west here but any guidance on this is appreciated.