2

Apex Jobs can be monitored from salesforce but as per the requirement I need to display this in a report to make sure which Job has executed, failed, rescheduled for next time, Job Name and ID.

Option 1: I am looking for reports where I can display all these fields but did not see any.

Option 2: Was trying to check in CronTrigger / CronJobDetail but not all the fields are there. Checked in AsyncApexJob but not all the fields are there. Can try wrapper class here.

Any idea if I can have this using general Reports without going for customized code?

  • It doesn't seem possible without code. Can you specify what information you are looking for, which these objects doesn't provide. maybe we can help in that direction. – Mr.Frodo May 26 '18 at 13:47
  • @Mr.Frodo - I am trying if I can get from admin report which I could not see. Using apex code I think I have to use wrapper class to retrieve all the info like job name, job I’d, no of failures/success, next scheduled time etc. – HRZ May 26 '18 at 16:56
1

Since, Salesforce doesn't provide OOTB reports on monitoring of jobs, you need to go for custom solution.

Create a separate object which will contain list of fields which you like to show in the report.

Create the newly created records of this object based on the jobId.

Refer Monitor Asynchronous Apex trailhead

If you’re performing this query inside the execute method of your schedulable class, you can obtain the ID of the current job by calling getTriggerId on the SchedulableContext argument variable.

global class DoAwesomeStuff implements Schedulable {

    global void execute(SchedulableContext sc) {
        // some awesome code
        CronTrigger ct = [SELECT TimesTriggered, NextFireTime FROM CronTrigger WHERE Id = :sc.getTriggerId()];
    }

}

You can also get the job’s name and the job’s type from the CronJobDetail record associated with the CronTrigger record. To do so, use the CronJobDetail relationship when performing a query on CronTrigger. This example retrieves the most recent CronTrigger record with the job name and type from CronJobDetail.

CronTrigger job = [SELECT Id, CronJobDetail.Id, CronJobDetail.Name, CronJobDetail.JobType 
FROM CronTrigger 
ORDER BY CreatedDate DESC LIMIT 1];

Alternatively, you can query CronJobDetail directly to get the job’s name and type. The following example gets the job’s name and type for the CronTrigger record queried in the previous example. The corresponding CronJobDetail record ID is obtained by the CronJobDetail.Id expression on the CronTrigger record.

CronJobDetail ctd = [SELECT Id, Name, JobType 
FROM CronJobDetail WHERE Id = :job.CronJobDetail.Id];
  • Thank you @Santanu Boral. Nice approach using custom Object and fields. I have done this using cronjobdetail and Crontrigger SObject on VF page with custom controller for now. One Question : is there anyway we can relate Crontrigger with the AsyncApexJob SObject? I did not see any direct relationship but trying to get related details of Crontrigger from AsyncApexJob. Pls suggest. – HRZ May 28 '18 at 8:29

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.