2

I have two scheduled apex jobs ScheduleJob1 and ScheduleJob2. Both these jobs call an apex class implementing the Schedulable interface

global class TaskController implements Schedulable {

     global void execute(SchedulableContext sc) {
            // code
     }
}

Now ScheduleJob1 is schedule to run every monday, whereas ScheduleJob2 is schedule to run first friday of every month.

In the execute method of TaskController depending upon which scheduled job called it, I want to perform separate code logic.

So something like this

global void execute(SchedulableContext sc) {

     if(ScheduleJob1 called this class) {
          //code logic 1
     } else if (ScheduleJob2 called this class) {
         //code logic 2
     }
}

I know we can get the ID of the CronTrigger scheduled job using getTriggerId() of the SchedulableContext interface. But how do I associate that Id with the Name of the job.

Can I do something like this:

global void execute(SchedulableContext sc) {
     List<CronTrigger> cronTriggerList = [select CronJobDetail.Name from CronTrigger where CronJobDetail.Id = :sc.getTriggerId()];

     if(cronTriggerList[0].CronJobDetail.Name == 'ScheduleJob1') {
          //code logic 1
     } else if (cronTriggerList[0].CronJobDetail.Name == 'ScheduleJob2') {
         //code logic 2
     }
}
  • 2
    Why not just have 2 separate classes if they are doing different things? It will make a lot more sense to the next developer that comes along rather than trying to save a couple lines of code to mash it all together. – dphil Aug 18 '14 at 18:03
  • they basically work on the same object with different results. Their inital working would be same, so I decided to put them in same class. – codeinprogress Aug 18 '14 at 18:19
  • 1
    How about use a shared method that returns something to work with? Sorry, I don't have an exact answer to your question. I haven't done anything like that. I just feel these might be the better solutions though in place of that. – dphil Aug 18 '14 at 18:20
  • You could have 1 class that does the processing, and 2 schedulable classes. Each sched class calls the same processing class, but with a param that tells it which thing to do. – Dominic Aug 18 '14 at 20:52
3

I would suggest adding it as an input variable:

global class TaskController implements Schedulable {

    String thisInput;

    global TaskController(String input) {
        thisInput = input;
    }

    global void execute(SchedulableContext sc) {
        if(thisInput == 'ScheduleJob1') {
            //code logic 1
        } else if (thisInput == 'ScheduleJob2') {
            //code logic 2
        }
    }

}

... then you'd schedule it like:

System.schedule('ScheduleJob1_20140818', '0 0 1 ? * 2', new TaskController('ScheduleJob1'));
0

This is mostly covered in the Apex docs for System.schedule

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

Notably, you can create two scheduled jobs with two different 'logical names' but both are executing the same Scheduled Apex class.

System.schedule('ScheduleJob1', '0 0 1 ? * 2', new TaskController());

Within the execute method, query the CronTrigger table for the CronJobDetail relationship:

SELECT CronExpression, CronJobDetailId, CronJobDetail.Name, Id, State FROM CronTrigger WHERE Id = sc.getTriggerId()

CronJobDetail.Name is your logical name

  • Can you do this when you schedule the job using the SFDC UI rather than scheduling from Apex? – Dominic Aug 18 '14 at 22:30
  • No, you can't specify the Job Name when using the SFDC UI; only using System.schedule – daveespo Aug 19 '14 at 2:14
  • Not sure if that was the case in 2014, but today you can specify a job name when scheduling an apex class from the UI. – Ralph Callaway Oct 30 '18 at 23:01

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.