1

I have just found a very strange behavior regarding Batch.

If I have a Batch and run it using the method Database.executeBatch(), a new record inside AsyncApexJob table is created and I can use ApexClassId field to find it using my class name.

For the Schedulable I have the same behavior regarding AsyncApexJob table and I have a new record inside the CronTrigger table.

Instead, if I have a Batch and I decide to schedule it using the method System.scheduleBatch() I receive a new record only inside CronTrigger and nothing inside AsyncApexJob!

In this case, I am not able to find if a specific Batch is "scheduled" or not because using the record inside CronTrigger table I am not able to retrieve info about that class.

Do you have any suggestion?

1 Answer 1

2

You cannot get class details for scheduled Apex jobs, as you say. What you can do is get the name of the scheduled job, which you can use to persist details your code can look for.

SELECT Id FROM CronTrigger WHERE CronJobDetail.Name = 'MY_JOB_STANDARD_NAME'

should do the trick.

When you schedule the batch, just pass some standard string to System.scheduleBatch() as its second parameter.

System.scheduleBatch(myBatch, 'MY_JOB_STANDARD_NAME', 200);

Then your code can always know whether or not its job is currently scheduled. You could even use your Apex Class Id as the job name, if you desire.

You must log in to answer this question.

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