• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

What is Database. Stateful In Batch Apex In Salesforce

  • February 23, 2024
  • Tech Blogger
  • Merfantz Developments, Salesforce Development & Implementation, Salesforce Developments Tutorial
  • 0

Introduction :

In Salesforce, Batch Apex is a powerful tool for processing large volumes of data asynchronously. It allows developers to efficiently handle data-intensive operations such as data cleansing, data migration, and complex calculations. However, when working with Batch Apex, it’s crucial to understand the concept of stateful and stateless processing, particularly in the context of database operations.

Stateful Processing in Batch Apex:

When a Batch Apex class is defined as “stateful,” it means that the class maintains state across multiple batches of records during the execution of a job. In other words, stateful Batch Apex classes retain variable values between each execution of the execute method within the same job instance. This capability is particularly useful when you need to maintain context or carry over information from one batch to another.
Understanding the significance of stateful processing in Batch Apex within the Salesforce database environment is essential for designing efficient and scalable solutions. It enables developers to leverage the full potential of Batch Apex for handling complex data processing tasks while ensuring data consistency and integrity across multiple batches.

Syntax for Batch:

global class BatchName implements Database.Batchable{}

Methods of Batch Class:

global (Database.QueryLocator | Iterable) start (Database.BatchableContext bc) {

// In this context, we will incorporate the query and then provide the result back to the execute method.}

global void execute (Database.BatchableContext BC, list<P>){

//Logic must be here

}

global void finish (Database.BatchableContext BC){

// Following the processing of all batches, this method will be invoked, allowing the inclusion of any desired code within it. For instance, one can use it to send emails to records created or updated in the execute method.}

Implementing the Database.Batchable Interface

Example:

global class BatchAccountUpdate implements Database.Batchable{

String query = ‘Select Name from Account WHERE Name! = null AND (Name = \’Sample Account\’ OR Name = \’Test Account\’) ‘;

global Database.QueryLocator start (Database.BatchableContext bc) {

// collect the batches of records or objects to be passed to execute

return Database.getQueryLocator(query);

}

global void execute (Database.BatchableContext bc, List records) {

// process each batch of records

for (Account acc : records){

acc.Name = acc.Name + ‘  Updated’;

}

update records;

}

global void finish (Database.BatchableContext bc){

// execute any post-processing operations, Calling batch class.

BatchUpdateAccountRelatedContacts b = new BatchUpdateAccountRelatedContacts();

Database.executeBatch(b, 200);

}

}

The provided sample class identifies all account records passed through the start() method using a QueryLocator. Subsequently, it updates associated contacts with their respective account’s mailing address. Lastly, it sends an email containing the bulk job results, and due to the utilization of Database.Stateful for state tracking, it includes information on the number of updated records.

global class SummarizeAccountTotal implements Database.Batchable<sObject>, Database.Stateful{

global final String Query;

global integer Summary;

global SummarizeAccountTotal(String q){

Query=q;

Summary = 0;

}

global Database.QueryLocator start(Database.BatchableContext BC){

return Database.getQueryLocator(query);

}

global void execute (

Database.BatchableContext BC,

List<sObject> scope){

for(sObject s : scope){

Summary = Integer.valueOf(s.get(‘total__c’))+Summary;

}

}

global void finish(Database.BatchableContext BC){

}

}

Conclusion

Utilizing Database Stateful in Batch Apex within Salesforce enables the retention of state across various execution methods within a batch job. This becomes essential in situations demanding intricate data processing, where maintaining state between batch chunks is crucial.

Adhering to best practices involves minimizing state variables and concentrating on essential data, thereby transforming Database.Stateful into a potent tool for intricate, state-dependent batch processing in Salesforce.

 

Related Links:

https://www.merfantz.com/blog/part-iv-7-batch-class-creation-in-salesforce/

 

https://www.merfantz.com/blog/writing-test-class-for-batch-class/

Author Bio

Tech Blogger
+ Recent Posts
  • Comprehensive Overview of Salesforce Agent-Force Platform
    June 12, 2025
    Comprehensive Overview of Salesforce Agent-Force Platform
  • Salesforce GraphQL Revolutionizing Data Queries in CRM
    June 6, 2025
    Salesforce GraphQL: Revolutionizing Data Queries in CRM
  • Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
    May 29, 2025
    Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
  • Einstein Activity Capture Boosting Productivity & Relationship Intelligence
    May 22, 2025
    Einstein Activity Capture: Boosting Productivity & Relationship Intelligence
Tags: Salesforce Development & ImplementationSalesforce Features
  • Previous How To Use Repeater Component (Beta) in Screen Flow
  • Next How to Create a Task Reminder Based on Due Date using Flow
Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

Comprehensive Overview of Salesforce Agent-Force Platform
Comprehensive Overview of Salesforce Agent-Force Platform June 12, 2025
Salesforce GraphQL Revolutionizing Data Queries in CRM
Salesforce GraphQL: Revolutionizing Data Queries in CRM June 6, 2025
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder May 29, 2025

Copyright @2023 Merfantz Technologies, All rights reserved