Using batch class to update record’s field and send email notification in finish method on custom Object in Salesforce | batch class to update records on custom object in salesforce


Hey guys, today in this post we are going to learn about how to write batch class to update records/fields and send email notification in finish method on custom Object in Salesforce

Real time scenarios:- Write a batch class on Custom Object(scoreCard__c) to update Record’s Field through Anonymous Window in Salesforce.

Files we used in this post example

batch_scoreCardEmailUpdate.apxc Apex Class Controller It is used for update Record’s Field through Anonymous Window in Salesforce

Live Demo

batch class to update a field on custom object in salesforce --


You can download file directly from github by Click Here.



Other related post that would you like to learn in Salesforce


Create Apex Class Controller

Step 1:- Create Apex Class : batch_scoreCardEmailUpdate

From Developer Console >> File >> New >> Apex Class

batch_scoreCardEmailUpdate [Apex Class Controller]

  1.    global class batch_scoreCardEmailUpdate Implements DATABASE.Batchable<sobject> {
  2.     global DATABASE.QueryLocator START(DATABASE.BatchableContext BC){
  3.         string query = 'Select Id, Name, Email__c, City__c, Phone_No__c From scoreCard__c Where Name !=null';
  4.         RETURN DATABASE.getQueryLocator(query);        
  5.     }
  6.     global void EXECUTE(DATABASE.BatchableContext BC, List<scoreCard__c> scope){
  7.          List<scoreCard__c> scoreUpate = NEW List<scoreCard__c>(); 
  8.         FOR(scoreCard__c scoreObj: scope){
  9.             scoreObj.Email__c = '';
  10.             scoreObj.City__c = 'Delhi';
  11.             scoreUpate.add(scoreObj);
  12.         } 
  13.         system.debug('scoreUpate@@ ' + scoreUpate);
  14.         UPDATE scoreUpate;        
  15.     }
  17.     global void finish(DATABASE.BatchableContext BC){
  18.        AsyncApexJob a = [SELECT Id, STATUS, NumberOfErrors, JobItemsProcessed,
  19.           TotalJobItems, CreatedBy.Email
  20.           FROM AsyncApexJob WHERE Id =
  21.           :BC.getJobId()];
  22.        Messaging.SingleEmailMessage mail = NEW Messaging.SingleEmailMessage();
  23.        String[] toAddresses = NEW String[] {a.CreatedBy.Email};
  24.        mail.setToAddresses(toAddresses);
  25.        mail.setSubject('Apex Sharing Recalculation schedule email update' + a.Status);
  26.        mail.setPlainTextBody
  27.        ('The batch Apex job processed scoreCard update ' + a.TotalJobItems +
  28.        ' batches with '+ a.NumberOfErrors + ' failures.');
  29.        Messaging.sendEmail(NEW Messaging.SingleEmailMessage[] { mail });    
  30.     }
  32. }

Create a instance of batch class and execute Anonymous Window in Salesforce

Step 2:- Open Apex Class >> batch_scoreCardEmailUpdate >> Debug >> execute batch class in Anonymous Window in salesforce

From Developer Console >> Debug >> execute Anonymous Window

For Anonymous Window

  1.   batch_scoreCardEmailUpdate scoreCard_batch = NEW batch_scoreCardEmailUpdate();
  2.   DATABASE.executeBatch(scoreCard_batch,10);

batch class to update a field on custom object in salesforce --

Further post that would you like to learn in Salesforce




FAQ (Frequently Asked Questions)

How many records we can process using batch apex?

A maximum of 50 million records can be returned in the QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed.

How does batch Apex work?

Each time you invoke a batch class, the job is placed on the Apex job queue and is executed as a discrete transaction. This functionality has two awesome advantages: Every transaction starts with a new set of governor limits, making it easier to ensure that your code stays within the governor execution limits.

How do I test a batch Apex class?

When testing your batch Apex, you can test only one execution of the execute method. You can use the scope parameter of the executeBatch method to limit the number of records passed into the execute method to ensure that you aren't running into governor limits. The executeBatch method starts an asynchronous process.

Related Topics | You May Also Like

  • Our Free Courses β†’

Hi, This is Vijay Kumar behind the admin and founder of I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on

Leave a Comment