2

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

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

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'
  • 1
    And always remember, it's in the user's time zone, not GMT. – sfdcfox Mar 4 '15 at 21:18
  • To run it at 6:30 and 8:30, I used this: String sch = '0 30 6,8 * * ?'; – Vid L Oct 31 '15 at 1:07
  • Yes that would run it at 6:30am and 8:30am – BarCotter Nov 5 '15 at 17:46

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.