Skip to main content

Different ways to schedule Batch Class


1.      Write a schedulable class for your Batch class.

·         Lets us assume my batch class name is SFDC_ExecuteAccountBatch.
·         To schedule it we need to create a new class and implement a schedulable interface
·         Syntax:
global with sharing class SFDC_ExecuteAccountBatchScheduler implements Schedulable {
    global void execute(SchedulableContext sc) {
                 SFDC_ExecuteAccountBatch b = new SFDC_ExecuteAccountBatch();
               database.executebatch(b);
    }
}

2.      Write a batch class with Schedulable interface

·         To avoid writing extra class we can implement a schedulable interface in batch class to make it easy and maintainable.
·         Syntax:

global class SFDC_ExecuteAccountBatch implements Database.Batchable<SObject>, Schedulable {
                global Database.QueryLocator start(Database.BatchableContext BC) {
                                // query on  required object and return Database.QueryLocator
                }
                global void execute(Database.BatchableContext BC, 
                               List<Sobject> scope) { // execute method
                // for batch class
// do you processing here
                                          }

                                           global void execute(SchedulableContext sc){ // execute method for
                                                                // schedulable interface
                                                                //execute your batch class here
                `                                               SFDC_ExecuteAccountBatch b = 
                                                                          new SFDC_ExecuteAccountBatch();
                                                               database.executebatch(b);

                                            }
                                            global void finish(Database.BatchableContext BC) {
                                                                // do completion activity like sending email
                                            }
}

Important Batch Apex Governor Limits

  • Up to 5 batch jobs can be queued or active concurrently.
  • Up to 100 Holding batch jobs can be held in the Apex flex queue.
  • In a running test, you can submit a maximum of 5 batch jobs.

Best Practice

  • Use extreme care if you are planning to invoke a batch job from a trigger. You must be able to guarantee that the trigger does not add more batch jobs than the limit.
  • Use Database.Stateful with the class definition if you want to share instance member variables or data across job transactions.
  • Methods declared as future aren’t allowed in classes that implement the Database.Batchable interface.
  • Methods declared as future can’t be called from a batch Apex class.
  • All methods in the class must be defined as global or public.

References


Comments

Popular posts from this blog

Salesforce lightning component e.force:navigateToURL

This article will explain how to use ‘e.force:navigateToURL’ lightning component to navigate the system to any URL (visualforce page, record, any custom URL). Syntax of ‘e.force:navigateToURL’ var urlEvent = $A.get("e.force:navigateToURL");         urlEvent.setParams({           "url": "/" + recordId ,             "isredirect": "true"         });         urlEvent.fire(); Description of the parameters, 1.        URl – Set this parameter to the required URL that you want to redirect, in this example ‘recordId’ is my variable with has the Id of the record I want to redirect. We can use static URL (e.g https://www.salesforce.com ) as well. 2.        Isredirect – This is optional, this flag will not...

Best practices for Data Loading in Salesforce

Data loading is a very common requirement in most of the salesforce projects. We need to load many master data and transactional data in production based on requirements that include thousands of records. In this article, I am going to list some best practices that we can follow and keep in mind while loading data in production. 1.     Organization-Wide Sharing defaults - While loading data in production, if objects have ‘Private’ OWD then it does additional processing in the background. Therefore, it is better to load data using read/write OWD and change it to ‘private’ after cutover. 2.     Workflow rules, validation rules, and triggers - If these tools are enabled in your production org while loading data, it can give many errors and it may slow down the process. Therefore, it is recommended to disable these tools before loading data.   3.     Bulk API Vs. SOAP API - SOAP API is useful for small data sets where...

How to start with salesforce

'Salesforce' is cloud computing company, which provides many services and uses many technology to deliver services. Salesforce is high demanding cloud computing technology that provides 'PAAS' to create your application on cloud and deliver to users.  But how to start with salesforce. This article will provide you basic idea to start with Salesforce if you are new to this technology.  If you are a developer, it will be very easy to jump in Salesforce technology and master it. How Salesforce is useful to deliver solution: Salesforce platform will help you to deliver CRM Applications. You can build CRM applications which can be used by businesses to track sales activity. Salesforce Platform is employed across all industries to build and launch game changing apps for every department in a company, including HR, IT, operations, finance, legal, and marketing.  Here is the example of some applications that you can build with help of salesforce. ...