function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rico RiveraRico Rivera 

Cannot access CronTriggerId from AsyncApexJob metadata

Trying to query what ApexClass was used by a CronTrigger by determining what Trigger executed an AsyncApexJob.

The AsyncApexJob shows the Apex Class and if I can query the CronTriggerId that executed that job, I can determine what job was using that Apex Class.

The SOAP API Developer Guide shows that the Metadata for AsyncApexJob has a field for CronTriggerId but when I attempted to query that info, I received an error that the column doesn't exist for CronTriggerId.

Best Answer chosen by Rico Rivera
Ronald FlisijnRonald Flisijn
Hi Rico,
I know exactly what you mean, quite annoying.
SELECT Id, CronTriggerId FROM ASyncApexJob
Works fine and yields results when executed from the query editor in DevConsole.
However, when executed from an Apex class, it yields a compile error.
(No such column 'CronTriggerId' on entity 'AsyncApexJob')

I found a related idea (https://ideas.salesforce.com/s/idea/a0B8W00000GdgQhUAJ/add-a-relationship-field-between-crontrigger-and-asyncapexjob), feel free to upvote it...


 

All Answers

Prasanthi_s1505Prasanthi_s1505
Hi Rico,

please find the below query where you can find required information..

SELECT id,CronJobDetailId,NextFireTime,PreviousFireTime,state,StartTime,EndTime,CronExpression,TimesTriggered,TimezoneSidKey FROM CronTrigger

SELECT id,CreatedDate,CreatedById,JobType,ApexClassId,Status,JobItemsProcessed,TotalJobItems,NumberOfErrors,CompletedDate,MethodName,ExtendedStatus,ParentJobId,LastProcessed,LastProcessedOffset FROM  AsyncApexJob

If it was helpful.,
Please Mark it as Best Answer.

Thanks,
Prasanthi
 
Rico RiveraRico Rivera

Hey Prasanthi,

Thanks for the response but that's not what I'm looking for. 

Essentially I want to be able to determine what Apex Class a CronTrigger is running.

I'd be able to determine what ApexClass a CronTrigger was running if there was a relationship between the AsyncApexJob and CronTrigger object. When looking it up, there's supposed to be a field called "CronTriggerId" on the AsyncApexJob object but I can't retrieve values from that field when I attempt to query and I get an error that says that field doesn't exist for the AsynApexJob object.

Prasanthi_s1505Prasanthi_s1505
Hi Rico,

please check this if it can help..

select ApexClass.Name, ApexClass.Namespaceprefix, Id, JobItemsProcessed, JobType, Status, NumberOfErrors, MethodName from AsyncApexJob

Use WHERE clause also to find on particular JOB.

Please mark it as Best answer if youu find useful.

Thanks,
Prasanthi
 
Rico RiveraRico Rivera

Hey Prasanthi,

That also doesn't tell me what CronTrigger was used to execute that Job.

To give you some more background, I tried to deactivate a Trigger that was firing a Job because it was running as an inactive user. The Trigger was poorly named to "Job2". When the error message came in through my email, it gave me the Job id and from that I was able to determine the Apex Class.

I was pretty sure it was the Trigger labelled "Job2" but I could not confirm what Apex Class Job2 was using because I couldn't not manage the Trigger from the "Scheduled Jobs" screen and I could not confirm from the Job what Trigger had executed it to run in the first place. 

Thanks, 

Rico Rivera

Prasanthi_s1505Prasanthi_s1505
Hi Rico,

We can also check directly in SETUP --> QFB --> APEX Flex Queue 

User-added image

Thanks,
Prasanthi
Prasanthi_s1505Prasanthi_s1505
Hi Rico,

Cron trigger is related to schedule job.

AsyncApexJob is related to asynchronous jobs...

Thank You,
Prasanthi
Rico RiveraRico Rivera
Yeah, I'm aware of that. The CronTrigger is a scheduled job and the AsyncApexJob is an Apex job.

I'm trying to find a relation between CronTrigger and AsyncApexJob via SOQL. I know they are directly related because the trigger creates the AsyncApexJob but there's nothing I can query to show what trigger fired what job. Looking at the SOAP API developer guide, there should be a field to query on the AsyncApexJob carried CronTriggerId but I cannot query that info.
Ronald FlisijnRonald Flisijn
Hi Rico,
I know exactly what you mean, quite annoying.
SELECT Id, CronTriggerId FROM ASyncApexJob
Works fine and yields results when executed from the query editor in DevConsole.
However, when executed from an Apex class, it yields a compile error.
(No such column 'CronTriggerId' on entity 'AsyncApexJob')

I found a related idea (https://ideas.salesforce.com/s/idea/a0B8W00000GdgQhUAJ/add-a-relationship-field-between-crontrigger-and-asyncapexjob), feel free to upvote it...


 
This was selected as the best answer
Rico RiveraRico Rivera

Thanks Ronald,

Definitely not optimal but a solution I hadn't realized. I'll do that for future queries where I run into a similiar issue.

Luckily I don't need to perform that Query in a class currently, I was attempting to audit our automations and couldn't accurately do so. I was using a third party chrome extension called Salesforce Inspector rather than the Dev Console.