3

I am new to scheduled batch apex jobs. I am trying to run a job every day at 10 AM and 10 PM but the logic doesn't seem to be working. Here's what I have so far

Time currentTime = Datetime.now().time();

Time desiredStartTime = Time.newInstance(10, 0, 0, 0);

Integer minutesFromNow = 0;

minutesFromNow = ((desiredStartTime.hour() * 60) + desiredStartTime.minute()) - ((currentTime.hour() * 60) + currentTime.minute());

if (minutesFromNow <= 0) minutesFromNow = minutesFromNow + 720;

When it tries to run at 10PM it gives me this error - First error: minutesFromNow must be non-negative

2
  • How are you scheduling the job? Are you using System.schedule
    – BarCotter
    Commented Mar 4, 2015 at 17:34
  • Yes.System.scheduleBatch(new classname(), JOB_NAME, minutesFromNow, 10);
    – sfnoob
    Commented Mar 4, 2015 at 17:37

1 Answer 1

3

Using the examples listed in System.schedule, you can schedule your job to run everyday at 10am and 10pm using the following code:

GXCalculateGranteeTotals job = new GXCalculateGranteeTotals();
String sch = '0 0 10,22 * * ?';
system.schedule(JOB_NAME, sch, job);

Or if you only wanted it on weekdays it would be:

'0 0 10,22 * * MON-FRI'
3
  • 1
    And always remember, it's in the user's time zone, not GMT.
    – sfdcfox
    Commented Mar 4, 2015 at 21:18
  • To run it at 6:30 and 8:30, I used this: String sch = '0 30 6,8 * * ?';
    – LVS
    Commented Oct 31, 2015 at 1:07
  • Yes that would run it at 6:30am and 8:30am
    – BarCotter
    Commented Nov 5, 2015 at 17:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .