April 15, 2015

Salesforce: Schedule schedulable job or batchable for next minute from anonymous apex script

Sometimes we have to check Schedulable Job or Batchable job execution when scheduled.

Also we don’t want to wait until specific date or time happened (assume on production your Schedulable or Batchable job should be scheduled to 1 a.m. of last day of the month, and you start working at 10 a.m. of average day of the month), you have to test it now if it won’t fail during the execution on scheduled time.

So you have solution: you can temporarily schedule your job to next minute in sandbox. Don’t forget to delete it later. However, web interface does not allow you to schedule job for a specific minute; only hour, day, month you can choose. Still you may do this from the anonymous apex script execution.

The following code snippet implements scheduling class called Name_Of_Your_Class_Implementing_Schedulable_Interface which should implement Schedulable interface. This snippet uses System.schedule method. Job will be shown on your scheduled job list (at URL.getSalesforceBaseUrl().toExternalForm() + ‘/08e’)  under name “Some fake string to be passed as job name, do not forget that it should be unique :)”.

1
2
3
4
5
6
Datetime dt = Datetime.now().addMinutes(1);
 
String sch = '0 ' + dt.minute() + ' * ' + dt.day() + ' ' + dt.month() + ' ' + ' ? ' + dt.year();
 
System.schedule( 'Some fake string to be passed as job name, do not forget that it should be unique : ) ', sch, new Name_Of_Your_Class_Implementing_Schedulable_Interface() );

The following code snippet implements scheduling class calledName_Of_Your_Class_Implementing_Batchable_Interface which should implement Batchable interface. Batch size will be 2000 for this snippet. This snippet uses
System.scheduleBatch method.

1
2
System.scheduleBatch(new Name_Of_Your_Class_Implementing_Batchable_Interface(), 'x', 1, 2000);

If you do not want to specify batch size, you may just omit that parameter and run this code snippet which uses This snippet uses
System.scheduleBatch method.

1
2
System.scheduleBatch(new Name_Of_Your_Class_Implementing_Batchable_Interface(), 'x', 1 );

In last option, default value of 200 as scope size will be used.

Also, if you want just to execute Batchable class without scheduling, you may use one of two overloaded versions of Database.executeBatch method like following

1
2
3
Database.executeBatch( new Name_Of_Your_Class_Implementing_Batchable_Interface() );
Database.executeBatch( new Name_Of_Your_Class_Implementing_Batchable_Interface(), 2000 );

You may or may not specify scope size as second optional parameter. If you do not specify scope size, default value of 200 will be used.

I hope you enjoyed reading this post and found something useful and learned something new.

You may click like to let me know that you did really enjoy 🙂

Tags



Share


Recent Articles

Women Driving Tech.
Interview with Intelligent, Classy, Ambitious and Inspirational Technologists.

April 18, 2019

We start a new blog series highlighting female forward-thinking technologists leading the way to innovations. The ladies of CoreValue are a part of European Women in Tech, and they regularly meet exceptional women at technology conferences and summits who shape the industry landscape and inspire others. Motivated by talking to the leading women in the […]

Migrate from Aura to Lightning Web Components to Increase Performance

April 12, 2019 | Bohdan Dovhan, Senior Salesforce Engineer

Salesforce cloud platform provides customers with the whole variety of options for customization. Those include point-and-click tools like Lightning App Builder, Process Builder, Visual Flow and Workflow, and development tools like Apex, Visualforce, Lightning Aura Components, Lightning Aura Events, Lightning Aura Tokens, Lightning Aura Standalone application and Lightning Web Components. While reading this list you […]

Contact Us

By submitting this form you acknowledge that you agreed to our Cookies and Privacy Policy.