Support Ukraine 🇺🇦 Help Provide Humanitarian Aid to Ukraine.
For Developers

Batch Apex Error Event - CloudAnswers Hackathon

5 min read
CloudAnswers photo
CloudAnswers
Share
TODO

What is a Hackathon?

A hackathon is an event usually put together by a tech organization. The event brings programmers together over a specific period to collaborate on a project. It is typically a competition that brings out the creative prowess of the individuals involved. To learn more about hackathons, check out our blog post on how and why we run hackathons. In this post we’ll highlight Paul Fox’s entry from the February hackathon focused specifically on the Salesforce Spring ‘21 release.

Batch Apex Error Handling

In Spring ‘21 Salesforce added the BatchApexErrorEvent to ISV applications (apps for the AppExchange). Since CloudAnswers has a handful of apps on the AppExchange, and is constantly building apps, Paul decided to build a library that would make this easier for us to use on all of our projects.

What is Batch Apex?

If the last paragraph was completely confusing, here’s a quick summary of how batch apex works and how batch apex error handling is accomplished with and without this functionality. If you’re a seasoned Apex developer, you can skip this section.

Hopefully you’re familiar with Apex, which is the language Salesforce uses for server-side code. Batch Apex is code that runs asynchronously and facilitates processing large volumes of records. For example, if you wanted to automatically merge all the duplicate contacts on a weekly basis you might write a batch class that queries all the duplicate record sets and merges them. If you tried to do this without batch apex you may hit a lot of Salesforce errors due to the amount of queries and other database changes that happen when you merge each group. With batch apex you can split it up into batches (one group of duplicates, for example) and use batch apex to process all of the batches and reduce the chance of hitting any limits.

The General Syntax of Batch Apex

Start Method- the start method defines what records to process and often initializes some data that is needed during execution.

Execute Method- This is typically where most of the logic resides, and this code determines what happens with each batch of records. It’s important to catch any errors here and then process them in the finalize method for most error handling.

Finish Method – this is mostly the post-processing details. Usually this is where we put our error handling, creating records or sending an email whenever something happens.

Error Handling in Batches

The best practice when writing batch apex is to try and anticipate any errors that could happen and catch them within the execute or finish methods and handle them appropriately. However, there are two main problems with that:

  1. Not all errors can be caught
  2. No developer is perfect and can anticipate every error, especially since new problems can be added with any release or package installation.

So, what’s a developer to do? Well, check out BatchApexErrorEvent.

Features of Batch Apex Error Event

  • It is a platform event that contains details about asynchronous processes that failed (batch and scheduled jobs).
  • It is automatically created if an asynchronous process fails, as long as that class implements Database.RaisesPlatformEvents.

Building a Batch Error Framework

Since platform events allow triggers and other workflows, that means we can automatically have logic fire when these platform events are created. Looking at how a lot of people do error handling already, Paul came up with the following features that most people would want when doing batch error handling.

  • Email batch errors to the admin or support team for evaluation and processing.
  • Stores batch errors in a custom object, including the class that failed, full stack trace, and the records that were in the Batch.

And all that with just a few set up steps.

Setup Instructions

  • Deploy the contents of the batch apex error handling repo into your org.
  • In the batch class of interest, implement Database.RaisesPlatformEvents. This causes the BatchApexErrorEvent to fire when something fails.
public with sharing class YourSampleBatchJob implements

Database.Batchable<SObject>, 
Database.RaisesPlatformEvents { 
  // class implementation 
} 
  • Set up at least one org-wide email address; you'll need to use this from the address on the emails.
  • Go to Setup > Custom Settings and create a new record for the Batch Apex Error Settings and specify how you want the errors to be handled.

That’s it, now you’re on your way to better error handling!

Using the Tool

Once you get a batch to fail, you should get an email or have a record created with the error details. If you set it up to create custom objects then check out the Batch Apex Error tab to see the errors, otherwise check your email.

If you're interested in seeing how you could customize this product for your organization, contact us, and let's see what we can build!

Lastly, if you haven’t seen them yet, check out some of the apps that we developed for Salesforce (some of these will be using this code in the future)


CloudAnswers photo
CloudAnswers
Share

About CloudAnswers

Salesforce apps, powerful components, custom development, and consulting. Our experienced team helps you to create and modify workflow processes in salesforce.

Related Articles

For Developers

Tips for Becoming a Salesforce Developer

Interested in becoming a Salesforce developer? In this blog post Jagmohan has put together his favorite tips and resources to get started in the world of Salesforce development.

April 4, 2024

6 Min Read

For Developers

Designing User Security and Visibility in Salesforce

Trust and security are at the top of Salesforce's priority list. The platform has everything you need if you're looking to construct a robust user security paradigm. However, this security approach has flaws that an attacker can exploit to gain access to your data. The Salesforce Architect has the duty to ensure that these features are set up correctly.

March 16, 2022

7 Min Read

For Developers

Save DOM Element As Image Attachment In Salesforce

Use a dom-to-image Javascript library that can turn arbitrary DOM nodes into a vector (SVG) or raster (PNG or JPEG) image in Salesforce.

April 8, 2021

3 Min Read