How to use AdvancedCronBuilderAndScheduler to schedule job in Salesforce
Ashish Chauhan | 28-05-2023
AdvancedCronBuilderAndScheduler is a powerful tool that provides a user-friendly, GUI-based interface for scheduling Apex jobs. With this tool, you can easily generate CRON expressions and schedule your Apex Schedulable classes without the need for manual coding.
You can find AdvancedCronBuilderAndScheduler repository at https://github.com/code-infokalpa/AdvancedCronBuilderAndScheduler
To try out AdvancedCronBuilderAndScheduler in a Salesforce scratch org, follow the steps below:
1. Clone the repository:
git clone https://github.com/code-infokalpa/AdvancedCronBuilderAndScheduler
cd AdvancedCronBuilderAndScheduler
2. Create a scratch org:
sfdx force:org:create -s -f config/project-scratch-def.json -a ACBS_SO -v <dev_hub_alias>
Note: 1) Replace <dev_hub_alias> with your dev hub alias. 2) ACBS_SO is alias of scratch org, you can change as per your preference.
3. Push the source to the scratch org:
sfdx force:source:push
4. Open the org using the command:
sfdx force:org:open
Now that the AdvancedCronBuilderAndScheduler app is deployed in your scratch org, you can proceed to create a sample Schedulable Apex class. You can either use the below example or create your own:
public class DummyScheduledClass implements Schedulable {
public void execute(SchedulableContext context) {
// Your logic or actions to be performed at the scheduled time
// Add your code here
System.debug('Scheduled class executed successfully!');
}
}
By following these steps, you can leverage the capabilities of AdvancedCronBuilderAndScheduler to easily schedule your Apex jobs using a point-and-click interface. This tool simplifies the process and allows you to focus on the logic and functionality of your scheduled classes.
To access the Visualforce page, follow these steps:
Open your browser and enter the following URL, replacing <scratch_org_subdomain> with your scratch org subdomain:
https://<scratch_org_subdomain>.scratch.vf.force.com/apex/AdvancedCronBuilderAndScheduler
This URL will direct you to the AdvancedCronBuilderAndScheduler Visualforce page, where you can utilize its scheduling capabilities.
Here is a screenshot of the rendered Visualforce page:
With AdvancedCronBuilderAndScheduler, you have the flexibility to configure various scheduling parameters, including seconds, minutes, hours, days, months, and years, for your Apex jobs.
The tool also generates the corresponding CRON expression based on your selected options. If you prefer, you can manually copy the generated CRON expression and use it to schedule your Apex class using the Developer Console.
Furthermore, on the Visualforce page, you’ll find a dropdown menu that lists all the Apex classes implementing the Schedulable interface. Simply select the desired class from the dropdown and click the “Schedule” button to schedule it with the generated CRON expression.
Let’s consider a specific use case where we want to schedule an Apex job to run on specific dates each month, such as the 14th, 15th, and 16th, at 2:30 AM.
If you want to use the standard features of the Salesforce Apex job scheduler interface, it’s important to note that there are a couple of limitations. Firstly, you cannot schedule jobs to run at specific minutes like 15 or 30 within an hour; you can only schedule them at the 0th minute of any given hour. Secondly, it is not possible to schedule a single job to run on multiple dates within a month. Instead, you would need to create multiple jobs, each scheduled for a specific date.
Lets see how we can do this using AdvancedCronBuilderAndScheduler.
- Select 0th seconds on “Seconds” tab
- Select 30th minutes on “Minuts” tab, like shown below
- Select ‘02’ of specific hours radio button under “Hours” tab, like shown below
- Select “Specific day of month” radio button select 14, 15 and 16 under “Day” tab as shown in below screenshot.
You can keep the Month and Year tab settings as it is because we want to execute job for all the months and years, until we stop it manually.
You can cross check the next scheduled date/time in “Scheduled Job” under setup, which should be as shown below.
Conclusion: AdvancedCronBuilderAndScheduler simplifies the process of constructing complex CRON expressions, making it effortless to schedule any Apex job based on your desired schedule.
If you have any further questions or need assistance, feel free to reach out to me at ashish.ch@infokalpa.com.
Thank you for your time and consideration.