4

Roughly I know that when I schedule a Schedulable, I'm creating both a CronTrigger and an AsyncApexJob, but I'm confused as to how the two objects relate to each other and what the significance of each is.

For example, if I'm looking at all scheduled jobs, I can look at the unfired triggers or filter on status for the async jobs. What are the pros and cons of examining one over examining the other?

2 Answers 2

8

CronTrigger is the parent job that will be run; it can have many runs attached to it. AsyncApexJob is a single job, including batchable, queueable, and future methods. So, if you want to know what's coming up, CronTrigger will let you know what's scheduled, while AsyncApexJob shows prior executions. If you want to know what's already run, AsyncApexJob is the the best choice, while if you want to know what's coming up, CronTrigger is the best.

2
  • 1
    Rewording so I can make sure I understand. For running and already run, use refer to AsynApexJobs. For future runs, refer to CronTriggers.
    – user823447
    Jun 7, 2018 at 16:44
  • 1
    @user823447 Exactly. AsyncApexJob is any type of asynchronous job, and the CronTrigger is the item that shows up in the Scheduled Jobs list.
    – sfdcfox
    Jun 7, 2018 at 16:52
-1

According to salesforce doc

AsyncApexJob
  Represents an individual Apex sharing recalculation job, a batch 
  Apex job, a method with the future annotation, or a job that 
  implements Queueable.

CronTrigger
  Contains schedule information for a scheduled job. CronTrigger is 
  similar to a cron job on UNIX systems.

From the documentation it seems that ApexSyncJob should be used to get information on the status of the last run, if there were any errors, how many batches were processed, and general information about the job.

Crontrigger provides more specific information about the scheduling of the job and when is the next run etc.

Thus depends on the information you want to verify.

You must log in to answer this question.

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