batch apex example

Batch apex class can be used to process record step by step. Like, for example you want to update 5,000 account records in steps of 200 each, you can use batch apex for this. You can schedule a batch apex class by using scheduler class.

There are 3 methods of a batch apex class:

  •    Start method
  •    Execute Method
  •    Finish Method


Start method is called at the start of the batch Apex. Start method collects all the records using the query.

Execute method is called after the start method, it receives the list of record which can be processed as required.

Finish method is called at last after the batch process is completed. Finish method can be left blank if not required or can be used to send the email confirmations.

Let us write a simple batch apex class and also schedule it.
Requirement: Write a batch apex class to update all the account records daily(update field description on account)

                                                  Class



                                          Scheduler class



The start method queries all the accounts and passes these records in batches of 200 records per batch to execute method in list "Scope" [List<account> scope] where these records are processed(Updated).

How to schedule this class?

Go to set up -> Apex Classes --> Press button "Schedule Apex"

Select your Apex scheduler class "UpdateAccountBatch_Schedular" in the look up and select the time and frequency as required.

































How to check the scheduled job ?

Go to set up -> Monitoring -> Scheduled Jobs

3 comments:

  1. Hi,
    How to implement above image dynamically from visualforce page?

    help me...........

    ReplyDelete
  2. Nice explanation,, good work!!
    here another great topic about salesforce!! hope you will like it.

    http://www.engineeringway.com/2018/02/set-up-single-sign-on-for-your-internal.html

    ReplyDelete