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
AbiBrownieAbiBrownie 

Schedule execution of an apex class based on user input

I have a visualforce page in which user will specify time in seconds(say 180seconds) and using this time , a class should run in intervals of the specified time. That is it should run for every 3 minutes if  the user enters 180 seconds in visualforce page and saves. In that class I use call outs to fetch data from external database and update it here. Can you please provide me a solution for this scenario?

bob_buzzardbob_buzzard

You may be able to do this via scheduled apex - use a cron string to schedule the execution in 3 minutes time.  Then in your scheduled apex class, its final action is to schedule itself for 3 minutes time.  I haven't tried to this level of granularity though, so I'm not sure if you can schedule jobs this close together.  One more important point is that you are only requesting a start time, but depending on resource availability it may not run for some time (we've seen up to 45 minutes difference in the past).

 

The other way to approach it is to set up an actionpoller that causes the user's browser to execute an action method in your controller every 3 minutes.  You could activate this from a popup window or similar, but it does mean that the user mustn't shut down their browser or computer.

dev401hasdev401has

Yes. As said by bob, u will have to create a cron string on the base of the value entered by user. after preparing the cron string just simply schedule your apex class

System.schedule('name of job',cron string, method()); this will schedule it.

You can cross check that by querying in CronTrigger API object that it has been scheduled.