0

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.

3
  • You pretty much put in your answer. @future is limited for the reasons you put. Scheduled Apex won't update regularly. The only way I can think of is doing some web service or API every 30 min or so to query recently updated opportunities (assuming there is a roll up summary) and do a check there. Commented Feb 28, 2019 at 21:31
  • 1
    Thanks for the response @disasterlooms . do you think this might help -help.salesforce.com/articleView?id=000002809&type=1
    – Sam
    Commented Feb 28, 2019 at 21:35
  • It is worth a try. I have never used that in a schedule and am not familiar with the documentation. If it does work, then I may use it for some other clean up items I need to do. My biggest concern though would be with recurring triggers. The delay update may cause another trigger and then a cycle. Commented Mar 1, 2019 at 17:40

0

You must log in to answer this question.

Browse other questions tagged .