I want to run an apex code every 30 seconds and want to use Scheduled Jobs. However, there are limitations on it. So far I have read the following options: 1- Run it on another server (e.g., unix) 2- Use multiple Scheduled jobs that run independently with specific intervals 3- Chain scheduled jobs by creating a new one after the old one is done.

All of these seem to work but are workarounds. Some of them seem to be old too. What's the best way to do this? Is there a better way?

  • 1
    In Salesforce you can never be sure that a job starts exactly at the time you scheduled it. This can be even minutes off. That is related to the multitenant infrastructure of the system. Server loads are spread in time. So if you want to do something exactly every 30 secs, option 1 is your only option. – Guy Clairbois Oct 2 '14 at 20:01
  • In addition to @guy's comment. If a Scheduled Job is during a SalesForce maintenance window and SF have brought down that server, the Scheduled Job will not run until the server is brought back up – BarCotter Oct 3 '14 at 8:52

you can do it like below :

Datetime sysTime = System.now().addSeconds( 30 );
String chronExpression = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();

System.schedule( 'scheduledBatchable ' + sysTime, chronExpression, new scheduledBatchable() );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.