Skip to content

Instantly share code, notes, and snippets.

@victorabraham
Created September 17, 2021 04:32
Show Gist options
  • Save victorabraham/834f3eac25f49989e4fbc1d12c608da4 to your computer and use it in GitHub Desktop.
Save victorabraham/834f3eac25f49989e4fbc1d12c608da4 to your computer and use it in GitHub Desktop.
//This can be any time in future
Datetime nextRun = System.now().addHours(2);
//Generating cron expression for next run time
String year = String.valueOf(nextRun.year());
String day = String.valueOf(nextRun.day());
String hour = String.valueOf(nextRun.hour());
String min = String.valueOf(nextRun.minute());
String sec = String.valueOf(nextRun.second());
String cronExp = sec + ' ' + min + ' ' + hour + ' '+day+' '+nextRun.format('MMM').toUpperCase()+' ? '+year;
//Scheduling at the specified time
TrackerSchedulable trackerJob = new TrackerSchedulable();
System.schedule('Tracker Job', cronExp, trackerJob);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment