If the event was scheduled via the UI, you should be able to get the information you want via SOQL. Here's the problem: CronTrigger
has most of the data points you need, which you can access through CronJobDetail
if you query on Name
, which you can see. But the only way to determine which ApexClass
is scheduled is via AsyncApexJob
, which has no relationship to either SObject
.
An unofficial source claims that the CreatedDate
should match if the job was scheduled through the UI. So if that is the case, you should be able to get the data with two queries. The following worked for me in Execute Anonymous
to infer the necessary data:
CronTrigger jobData = [
SELECT CreatedDate, CronExpression, EndTime
FROM CronTrigger WHERE CronJobDetail.Name = 'Job I Want To Delete'
];
AsyncApexJob execution = [
SELECT ApexClass.Name FROM AsyncApexJob
WHERE CreatedDate = :jobData.CreatedDate
];
system.debug(jobData.EndTime);
system.debug(jobData.CronExpression);
system.debug(execution.ApexClass.Name);
From there, you can use System.schedule
, though it may get you or someone else into a deeper conundrum down the road.
Schedulable instance = (Schedulable)Type.forName(execution.ApexClass.Name).newInstance();
system.schedule('Job No Longer Deleted', jobData.CronExpression, instance);
However, it may be preferable to simply interpret the CronExpression
for yourself and still schedule through the UI. For example, if you scheduled it to execute at 1:00 PM each weekday, it would look like: 0 0 13 ? * 2,3,4,5,6
. If it's scheduled through the UI in the first place, then you will get the hour from the third component, and the days of the week from the final component. 1
indicates Sunday, 2
indicates Monday, etc.
Best Answer
You can get this information by querying the
CronTrigger
andCronJobDetail
objects:JobType
7 is Scheduled Apex.The
CronExpression
is the critical Cron timing string that you can pass toSystem.schedule()
, which allows for types of schedules that are not supported in the user-interface scheduler; that will let you rebuild the jobs on the same schedule as you expect.The easiest place I am aware of that you can see the Apex class name, frustratingly, is the job's detail page under Scheduled Jobs in Setup:
You can find Apex class names from the
AsyncApexJob
object, which represents the queued run of the scheduled job, but I'm not aware of any way other than visual inspection to correlate theAsyncApexJob
back to the parentCronTrigger
(itsParentJobId
is blank). Regardless, you can queryYou'll be able to view the actual class code only if it's not part of a managed package.
You can schedule a job programmatically by opening Anonymous Apex in the Developer Console and entering
The second argument is your
CronExpression
from theCronTrigger
query.