DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Salesforce Bulk API 2.0: Streamlining Large-Scale Data Operations
  • An Introduction to Stream Processing
  • High-Volume Security Analytics: Splunk vs. Flink for Rule-Based Incident Detection
  • Accelerate Innovation by Shifting Left FinOps: Part 4

Trending

  • Exactly-Once Processing: Myth vs Reality
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • Chaos Engineering Has a Blind Spot. Agentic AI Lives in It.
  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Introduction to Salesforce Batch Apex [Video]

Introduction to Salesforce Batch Apex [Video]

Explore core concepts and practical applications of Batch Apex in Salesforce, including the structure, how to write unit tests, scheduling, and more.

By 
Dino Scarcella user avatar
Dino Scarcella
·
Aug. 07, 24 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
4.6K Views

Join the DZone community and get the full member experience.

Join For Free

Salesforce Batch Apex is a powerful tool for handling large data volumes and complex data processing tasks asynchronously. This tutorial will walk you through the core concepts and practical applications of Batch Apex in Salesforce, including the structure of an Apex batch class, writing unit tests for batch classes, scheduling batch classes, running batch classes ad hoc for testing, and understanding Batch Apex limits.

Structure of an Apex Batch Class

An Apex Batch class in Salesforce must implement the Database.Batchable interface. This interface requires the implementation of three methods:

  1. Start method: This method collects the records or objects to be passed to the execute method. It can return either a Database.QueryLocator object or an iterable list of records.
  2. Execute method: This method contains the logic to process each batch of data passed from the start method. It is called for each batch of records.
  3. Finish method: This method is called after all batches are processed. It can be used to send notifications or perform any post-processing tasks.

Apex Batch Class Unit Test

Testing batch classes is crucial to ensure they work as expected. A unit test for a batch class typically involves:

  1. Setup test data: Creating the necessary test data within the test class
  2. Test the batch class: Calling the batch class within a test method and verifying the results; The test ensures that the batch class processes the records correctly and updates them as expected.

Batch Class Scheduling

Salesforce allows you to schedule a batch class to run at specific times using the System.schedule method. To do this, you need to:

  1. Implement the Schedulable interface: Create a class that implements the Schedulable interface and schedules the batch job.
  2. Schedule the batch job: Use the System.schedule method to set the desired schedule. This involves defining a cron expression that specifies when the job should run. Alternatively, just use the front end with the scheduled jobs menu in Setup, as I've shown in the video below.

Running Batch Classes Ad Hoc for Testing

To run a batch class ad hoc for testing purposes, you can directly call the Database.executeBatch method. This allows you to run the batch job immediately without scheduling it, making it useful for development and testing.

Batch Class Apex Limits

Understanding the limits is crucial when working with Batch Apex to avoid governor limit exceptions:

  1. Batch size: The maximum batch size is 2,000 records, but the optimal size is typically 200 to 500 records, depending on the complexity of the batch process.
  2. Total number of Batch Apex Jobs: Salesforce allows up to 5 active batch jobs simultaneously and up to 100 queued or active jobs.
  3. Total number of records processed: A single batch job can process up to 50 million records.
  4. Governor limits: Batch Apex execution is bound by various governor limits, such as the total number of DML statements, SOQL queries, CPU time, heap size, etc.

Video Tutorial



Conclusion

Salesforce Batch Apex is an essential feature for handling large-scale data processing efficiently. By understanding the structure of a batch class, writing comprehensive unit tests, scheduling batch jobs, running them ad hoc, and being mindful of the Apex limits, you can leverage Batch Apex to its fullest potential. Use this tutorial as a starting point to build and optimize your batch processes in Salesforce.

Feel free to reach out with any questions or further insights on Batch Apex in Salesforce. Happy coding!

Data (computing) Record (computer science) unit test Batch processing

Opinions expressed by DZone contributors are their own.

Related

  • Salesforce Bulk API 2.0: Streamlining Large-Scale Data Operations
  • An Introduction to Stream Processing
  • High-Volume Security Analytics: Splunk vs. Flink for Rule-Based Incident Detection
  • Accelerate Innovation by Shifting Left FinOps: Part 4

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook