Tag Archives: Scheduled Apex

Schedule An Apex Job To Run Every 5 Minutes

Sample Code:

YourScheduledApexClass obj = new YourScheduledApexClass();

String sch1 = '0 0 * * * ?';
System.schedule('Schedule Job1', sch1, obj);

String sch2 = '0 5 * * * ?';
System.schedule('Schedule Job2', sch2, obj);

String sch3 = '0 10 * * * ?';
System.schedule('Schedule Job3', sch3, obj);

String sch4 = '0 15 * * * ?';
System.schedule('Schedule Job4', sch4, obj);

String sch5 = '0 20 * * * ?';
System.schedule('Schedule Job5', sch5, obj);

String sch6 = '0 25 * * * ?';
System.schedule('Schedule Job6', sch6, obj);

String sch7 = '0 30 * * * ?';
System.schedule('Schedule Job7', sch7, obj);

String sch8 = '0 35 * * * ?';
System.schedule('Schedule Job8', sch8, obj);

String sch9 = '0 40 * * * ?';
System.schedule('Schedule Job9', sch9, obj);

String sch10 = '0 45 * * * ?';
System.schedule('Schedule Job10', sch10, obj);

String sch11 = '0 50 * * * ?';
System.schedule('Schedule Job11', sch11, obj);

String sch12 = '0 55 * * * ?';
System.schedule('Schedule Job12', sch12, obj);

Schedule An Apex Job To Run Every 15 Minutes

Sample Code:

YourScheduledApexClass obj = new YourScheduledApexClass();

String sch1 = '0 0 * * * ?';
System.schedule('Schedule Job1', sch1, obj);

String sch2 = '0 15 * * * ?';
System.schedule('Schedule Job2', sch2, obj);

String sch3 = '0 30 * * * ?';
System.schedule('Schedule Job3', sch3, obj);

String sch4 = '0 45 * * * ?';
System.schedule('Schedule Job4', sch4, obj);

Schedule An Apex Job To Run Every 10 Minutes

Sample code:

YourScheduledApexClass obj = new YourScheduledApexClass();

String sch1 = '0 0 * * * ?';
System.schedule('Schedule Job1', sch1, obj);

String sch2 = '0 10 * * * ?';
System.schedule('Schedule Job2', sch2, obj);

String sch3 = '0 20 * * * ?';
System.schedule('Schedule Job3', sch3, obj);

String sch4 = '0 30 * * * ?';
System.schedule('Schedule Job4', sch4, obj);

String sch5 = '0 40 * * * ?';
System.schedule('Schedule Job5', sch5, obj);

String sch6 = '0 50 * * * ?';
System.schedule('Schedule Job6', sch6, obj);

Salesforce Schedulable Apex Test Class

Batch Class:

global class AccountBatch implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT Id, Name, Industry FROM Account';                
        return Database.getQueryLocator(query);     
    }
    
    global void execute(Database.BatchableContext BC, List<Account> accList){        
        for(Account acc : accList){          
            acc.Industry = 'Banking';        
        }        
        update accList;       
    }
    
    global void finish(Database.BatchableContext BC) {
        
    }
}

Schedulable Class:

global class AccountBatchScheduled implements Schedulable {
    
    global void execute(SchedulableContext sc) {
        AccountBatch objBatch = new AccountBatch(); 
        Database.executebatch(objBatch);
    }
}

Test Class:

@isTest
private class AccountBatchScheduledTest{
    
    static testmethod void schedulerTest(){
        
        String cronexpression = '0 0 0 15 3 ? *';

        Account acc = new Account();
        acc.Name = 'Test Account';
        Insert acc;
        
        Test.startTest();
        String jobId = System.schedule('ScheduleBatchApexClass',  cronexpression, new AccountBatchScheduled());
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
        System.assertEquals(cronexpression, ct.CronExpression);
        System.assertEquals(0, ct.TimesTriggered);
        Test.stopTest();
    }
}