+ Start a Discussion
flewellsflewells 

Can Apex be used to create a custom time trigger?

I am trying to setup a workflow time trigger that will fire 1095 days (3 years) after a specified date. When I try to set this up, however, I get an error message that my number is too large. I logged a case with Customer Support to find out the maximum # days allowed...assuming I will not be able to set this up using point and click, could Apex serve as a substitute here? I know Apex can be used to update records immediately...can it be written to a handle time-based trigger too? Can you think of any other way to make this work?
Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

cool, now for the answer.

 

the solution will comprise of a trigger, a custom 'queue' object and scheduled apex.

 

the trigger will fire on insert / update of your record, and create / update a record in the custom 'queue' object. this will contain a date on which the scheduled apex should pick this record up for processing. the date will be caclulate as your record date + 1095.  So this trigger will fire each time your source record is changed, thus carrying the date change to your custom queue object.

 

Scheduled apex, perhaps scheduled to execute just after midnight every day, will scan this table for records where the processing date meets today. then pick those records up for processing and execute your logic.

 

in summary, apex will execute logic depending on how /what is written.

All Answers

Ritesh AswaneyRitesh Aswaney
Scheduled BatchApex could be a possible solution, coupled with a trigger on the object of interest, which creates a 'queue' record on creation of the record in a custom object.

The scheduled apex can check everyday that the number of days elapsed is greater than the threshold and if so, process as desired.
flewellsflewells
Thanks for the explanation, Ritesh. If my record is updated, is it safe to assume that the Apex can be written in a way so that the schedule is updated too? Also curious to know -- is this a fairly simple solution to implement? Just looking to do some high level cost/benefit analysis...
Ritesh AswaneyRitesh Aswaney
Hey not sure if I follow your question completely. I'd say this would be moderate complexity. Very achievable.
flewellsflewells
Sorry, I'll attempt to clarify. My goal is to have a field automatically updated to a specific value 1095 days after a date (this date is captured in a custom date field on my record). Let's say the date entered in my custom field is 04/20/2011. I want Apex to auto-update my status field to a specific value on 4/19/2014. But let's say 04/20/2011 is later corrected/changed to 04/15/2011. Now, instead of auto-updating my field on 4/19/2014, I want it to happen on 04/14/2014. I know this is how time based workflow triggers work. Just looking to confirm that Apex could do the same.
Ritesh AswaneyRitesh Aswaney

cool, now for the answer.

 

the solution will comprise of a trigger, a custom 'queue' object and scheduled apex.

 

the trigger will fire on insert / update of your record, and create / update a record in the custom 'queue' object. this will contain a date on which the scheduled apex should pick this record up for processing. the date will be caclulate as your record date + 1095.  So this trigger will fire each time your source record is changed, thus carrying the date change to your custom queue object.

 

Scheduled apex, perhaps scheduled to execute just after midnight every day, will scan this table for records where the processing date meets today. then pick those records up for processing and execute your logic.

 

in summary, apex will execute logic depending on how /what is written.

This was selected as the best answer
flewellsflewells
Got it. Thanks for the clear explanation.