Assume that our ORG has a custom object API named as "Product_Complaint__c".
Our aim is to schedule a batch apex which will responsible for make a select query on Product_Complaint__c object.
Our aim is to schedule a batch apex which will responsible for make a select query on Product_Complaint__c object.
/** * This is a batch class. * https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm **/ global class BatchClassExample implements Database.Batchable<sObject>{ global String query; global Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<sObject> scope){ List<Product_Complaint__c> lstOfPC = (List<Product_Complaint__c>)scope; // Call any helper calss to do the logic. System.debug('Test PC Size ::::::::::::: '); System.debug('PC Size ::::::::::::: '+ lstOfPC.size()); } global void finish(Database.BatchableContext BC){ } }
/** * This is a scheduler class example for batch apex. * This class implements the Schedulable interface for a class called AffectedScheculeClassExample. * * Link: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm **/ global class ScheduledBatchClassExample implements Schedulable { //This is implemented method and it must be declared as global or public. global void execute(SchedulableContext sc) { BatchClassExample batchInst = new BatchClassExample(); batchInst.query = 'SELECT Id FROM Product_Complaint__c'; database.executebatch(batchInst, 1); } }
/** * This class is responsible for calling ScheduledBatchClassExample class to run the scheduler. * Call this class constractor from anonymous window. * **/ public class CallScheduledBatchClassExample { //Constructor need to be call from annonymous window to run the schedule job. public CallScheduledBatchClassExample(){ ScheduledBatchClassExample schBatchClsExmpl = new ScheduledBatchClassExample(); /** * Schedule structure: * <Seconds> <Minutes> <Hours> <Day_of_month> <Month> <Day_of_week> <Optional_year> * * For below example it will schedulw the job @ 08:23 AM every day. */ String sch = '0 23 8 * * ?'; String schJobID = system.schedule('Scheduler Batch Example', sch, schBatchClsExmpl); } }
To test this example instantiate CallScheduledBatchClassExample()
from anonymous window as,
CallScheduledBatchClassExample inst = new CallScheduledBatchClassExample
();