function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Akshay JuwaAkshay Juwa 

Creating a batch class in Apex!!

Hello everyone! I'm trying to create a batch apex class for the following .
SELECT Id, PersonEmail, Brand__c, CreatedDate, Opt_Out_Preference__c FROM Account WHERE CreatedDate < 2022-09-15T16:00:53.000+0000 AND PersonEmail !=NULL AND Brand__c !=NULL AND Opt_Out_Preference__c = NULL AND Brand__c != 'Fragrance'

this is my first time using it..so I'd appreciate if you could guide me what to do next.
Best Answer chosen by Akshay Juwa
Ronak Toshniwal 9Ronak Toshniwal 9

first create a class

then to make it batch class you need to 

  • first make class global/public and implement interface database.batchable<sObject>
  • in this it is compulsory to write 3 methods 
  • first method is start return type database.querylocator or  Iterable<sObject> and "start" name of first method name of all methods should be as defined in example code name of class can be different and variable names inside any method can be different.
  • in start method you can write your query to get record
  • in execute method you can perform operation what ever you need to do
  • in finish method if not required you can leave this empty but defining is compulsory.
for more you can read 
 
global class AccountUpdateBatchJob implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        //get the records
      String query = 'SELECT Id,Name FROM Account';
       
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
     //process your records
     //perform operations
       

        for(Account a : scope)
        {
            a.Name = a.Name + 'Updated by Batch job';
        }
        update scope;
    }
    global void finish(Database.BatchableContext BC) {
      // //finish method is where you do your DML and commit your processed information 
           to Salesforce, this is for creating log records for your integration, and updating                       
           processed records to the database and chaining another batch class or even 
           schedule one.
}

    }
}

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Akshay,

What is the error you were getting?

If possible share the code.

Thanks!!
Ronak Toshniwal 9Ronak Toshniwal 9

first create a class

then to make it batch class you need to 

  • first make class global/public and implement interface database.batchable<sObject>
  • in this it is compulsory to write 3 methods 
  • first method is start return type database.querylocator or  Iterable<sObject> and "start" name of first method name of all methods should be as defined in example code name of class can be different and variable names inside any method can be different.
  • in start method you can write your query to get record
  • in execute method you can perform operation what ever you need to do
  • in finish method if not required you can leave this empty but defining is compulsory.
for more you can read 
 
global class AccountUpdateBatchJob implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        //get the records
      String query = 'SELECT Id,Name FROM Account';
       
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
     //process your records
     //perform operations
       

        for(Account a : scope)
        {
            a.Name = a.Name + 'Updated by Batch job';
        }
        update scope;
    }
    global void finish(Database.BatchableContext BC) {
      // //finish method is where you do your DML and commit your processed information 
           to Salesforce, this is for creating log records for your integration, and updating                       
           processed records to the database and chaining another batch class or even 
           schedule one.
}

    }
}
This was selected as the best answer
Akshay JuwaAkshay Juwa
Thank you.