Custom - Batch
On this page, you can find an example of building a Custom - Batch type job scheduler.
Step 1: Create Apex Class
You can easily generate groundwork for your custom scheduler job with Generate Scheduler Code.
Go to Breezz App → Breezz Setup → Scheduler Jobs → Generate Scheduler Code. As Job Type select Scheduler Job that run in the Batchable process, pick a class name and copy code to your IDE or Developer Console.
Let’s implement a simple job which will be executed every 30 minutes and create 5 account’s.
1public with sharing class DeletesAccountsSchedulerBatchJob extends forvendi.SchedulerQueryableBatch {
2 public String SOQL_QUERY = 'SELECT Id FROM Account WHERE CreatedDate = N_WEEKS_AGO:40';
3
4 public DeletesAccountsSchedulerBatchJob() {
5 super();
6 }
7
8 public override String prepareCountQuery() {
9 String query = SOQL_QUERY.replaceFirst('SELECT Id FROM ', 'SELECT Count() FROM ');
10 return (query.contains(' LIMIT ') ? query.left(query.lastIndexOfIgnoreCase(' LIMIT ')) : query) + ' LIMIT 1';
11 }
12
13 public override String prepareContextQuery() {
14 return SOQL_QUERY + ' LIMIT ' + 10000000;
15 }
16
17 public override void execute(SObject[] records, forvendi.ModificationContext ctx) {
18 ctx.addToRemove(records);
19 }
20}
The class which will be used in Scheduler must extend forvendi.SchedulerQueryableBatch and need to have 3 methods.
Method prepareCountQuery() which returns a query that should check if there is at least one record meeting criteria of the processing. A query is limited to 1 because we don’t care how many records, we care if there is at least on record and Count() value will be compare with integer value of 1 by Breezz internal logic to check whether the whole processing should be fired or not.
Method prepareContextQuery() returns a query that should return all the records to be processed by the Custom - Batch scheduler job.
Method execute() contains actual logic to be performed for the records determined by a query in prepareContextQuery().
In example above accounts that have been created 40 weeks ago are removed, scheduler for such logic should be executed on weekly basis.
Step 2: Create Test Class
1@IsTest
2private class DeletesAccountsSchedulerBatchJobTest {
3 @TestSetup
4 static void testSetup() {
5 // initializing Breezz setup,
6 // you have to provide forvendi.BreezzPlugins.BaseApexPlugin implementation if you would like to
7 forvendi.BreezzApi.TESTS.init('BreezzPlugin');
8 }
9
10 @IsTest
11 static void when_ExecutedAccountRecordsGenerator_expect_GenerateAccounts() {
12 // execute logic inside class that extends forvendi.SchedulerQueryableBatch
13 Test.startTest();
14 forvendi.BreezzApi.SCHEDULER.run('DeletesAccounts');
15 Test.stopTest();
16
17 List<AggregateResult> results = [SELECT Count(Id) amount FROM Account WHERE CreatedDate = N_WEEKS_AGO:40];
18 forvendi.BreezzAPI.TESTS.assertErrorLogs();
19
20 Assert.isTrue(results[0].get('amount') == 0);
21 }
22}
Step 3: Register Scheduler Job
After successfully deploying we can configure Scheduler. Go to: Breezz Setup → Scheduler Jobs → Click New.
Now we need to input Name and add some Description and select the type of Scheduler Job. In our case it will be Custom. Next select Execution Interval – 30 minutes. Lastly pick a class which implements forvendi.SchedulerJob you created or the one provided above.
Step 4: Check Scheduler Job
To check if a scheduler was created go to App Launcher → Breezz → Breezz Scheduler Jobs → All Custom Jobs.
Here you can see your Scheduler: