Today you are going to learn EXACTLY how to schedule APEX to run every 15 minutes. This can be useful when your data is altered by Guest Profile users. In such scenario, Guest Profile may not have access to other objects. Therefore you can’t use Process Builder or immediate triggers.
Step 1: Create Schedulable Apex
First of all, you have to have your apex class in a schedulable format. To achieve that, you need to create a new apex class with the following code:
You can read more about scheduling Apex on SalesForce Trailhead.
Let’s add some simple function to our scheduled apex.
The following script will search for Accounts that are:
- Last Modified today
- Not yet type ‘Prospect’
- Some custom checkbox is checked.
If such accounts are in the system, it will auto convert them to Type ‘Prospect’ everey 15 minutes.
Step 2: Scheduling Apex in a standard way
Go to Setup -> Apex Classes. From there you’ll be able to see that there is a button that is called ‘Schedule Apex‘. The truth is, Salesforce allows you to schedule APEX to run every hour.
From here you just need to select a day of the week, preferred time and you’ll be all set.
But what if you need your code to run every 15 minutes?
Step 3: Scheduling Apex via Developers Console
To open Developers Console click on Setup -> Developers console. The new window will pop up. From there you need to click on Debug – > Open Execute Anonymous Window
Here is the code for our scheduled item:
Here is what is happening above:
‘Account Updater: at 00 mins’, – we specify the name of our scheduled Apex;
‘0 0 * * * ?’, – Seconds, Minutes, Hours, Day_of_month, Month, Day_of_week, Optional_year
You can read more about this on Apex Developers Guide
This scheduled apex is set to run every hour at 00 minutes. Since we want to update records every 15 minutes, we just need to copy this 3 more times and change minutes accordingly.
The final script will look like this:
All you need is to paste the code above into Execute Anonymous Window and click ‘Execute‘. This will schedule apex to run every 15 minutes.
Please keep in mind that scheduled apex will auto reschedule itself to run again.
Step 4: Verify Schedule
Go to Setup -> Scheduled Jobs.
You should see something like this:
Now you know how to automate some of your business processes using apex that is running in the background every 15 minutes. Obviously, you can customize scheduled times to your needs. You’ve got the idea. Good luck!