Dynamic Apex Scheduling using Process Builder

We often face this problem where the GUI apex schedular does not solve our purpose where we want the schedular to be more dynamic and with more options. Which leave us with only one option run the schedular via code, which for a developers can be easy but not for other people.

I faced this problem and came up with a solution which is a combination of Apex class + Process Builder + An Object.

Step 1

Create an object with fields Seconds, Minutes, Hours, Day of Month, Month,Day of Week, Year, Name of Job (Standard Field Name), Name of Apex Class (Schedular Class), Active (Optional if you want more control you can use this field) 

It will be little difficult for non developers to understand Cron sequence in beginning but if you specify proper help text and validation rules they cannot go wrong. For example :-screen-shot-2016-10-08-at-8-48-53-pm

For reference checkout this link to understand the cron sequence better Apex Schedular

Step 2

Create a the class which will do the scheduling for you. Since this class will be called by process builder the method will be invocable.

 @InvocableMethod(label='Schedule Job Interface' description='Nothing')
 public static void scheduleJobs(List<MyscheduleObj__c> schedulers) {
 for(MyscheduleObj__c myObjRec : schedulers){

 // Get the type of the schedular class, Name which is populated in Name of Schedular Class field
    Type tp = Type.forName(myObjRec.ClassName__c);
 // Get the cron sequence make sure all the field are mandatory
    String sch = myObjRec.Seconds__c+' '+myObjRec.Minutes__c+' '+myObjRec.Hours__c+' '+myObjRec.Day_of_Month__c+' '+myObjRec.Month__c+' '+myObjRec.Day_of_Week__c+' '+myObjRec.Year__c; 
 CronTrigger conJob = new CronTrigger();

 conJob = [SELECT 
 FROM CronTrigger 
 // Use the Name as job name remember if you want modification later do not change the job name
 WHERE CronJobDetail.JobType = '7' AND CronJobDetail.Name = :myObjRec.Name];
 if(conJob != null){ 
 String jobID = System.schedule(myObjRec.Name, sch, (Schedulable)tp.newInstance()); 
 }catch(QueryException e){
 // there for the first time there is no scheduled jobs it will throw exception 
 // then you have to create the first job 
 String jobID = System.schedule(myObjRec.Name, sch, (Schedulable)tp.newInstance());
 }catch(Exception ex){
 system.debug('' + ex.getMessage());

I am not using the Active field if you want to activate and deactivate your scheduled job you can modify the logic.

Step 3 :-

Create process builder put any condition which you want on this object to call the class which contains this invocable method. It will take the object list as parameter which you can pass by choosing reference option and select the current object.

Step 4 :-

You should have a schedulable class, means class implementing Schedulable interface. For example :-

// Name of this class will be used in your className field of the object
global class MyScheduledClass implements Schedulable {
   global void execute(SchedulableContext SC) {
      // My Logic

And we are done !

You control multiple jobs and change their sequence whenever you want without any code.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s