When running multiple Apex jobs in Salesforce — whether using Schedulable or Batchable classes — errors can occur silently without any notification. Instead of manually checking the Apex Jobs list to verify that each job completed successfully, you can implement an automated email alert system that notifies you only when something goes wrong. This article describes how to set up error logging and email alerts for Salesforce Apex scheduled jobs.
The recommended approach uses two components: a custom object that stores error details when a job fails, and a Workflow Rule (or Flow) on that custom object that sends an email alert whenever a new error record is created. You receive a notification only when there is an actual error — you are not notified for every successful job run.
Create a custom object called Error_Log__c to store error information. At minimum, include a Long Text Area field called Trace__c to capture the full error trace. Optionally, break this into more specific fields — such as error type, error message, line number, and stack trace — for easier filtering and reporting.
In each Apex class that you schedule or batch, add a try-catch block around the main logic. In the catch block, create a new Error_Log__c record and populate the Trace__c field with relevant error details. The details to capture include:
e.getTypeName()e.getCause()e.getMessage()e.getLineNumber()e.getStackTraceString()You can also append custom variables from your class to the trace to provide additional context about what was being processed when the error occurred. Insert the Error_Log__c record using a DML insert statement.
Create a Workflow Rule on the Error_Log__c object with the evaluation criteria set to fire when a record is created. Add an Email Alert action that sends a notification to the appropriate team or administrator. Every time a new error log record is inserted — which only happens when an Apex job fails — an email is sent automatically.
try{
//Some DML statement
} catch (Exception e) {
Error_Log__c log = new Error_Log__c();
log.trace__c = 'Type: ' + e.getTypeName() + '\n' + 'Cause: ' + e.getCause() + '\n' + 'Message: '
+ e.getMessage() + '\n' + 'Line #: ' + e.getLineNumber() + '\n' + e.getStackTraceString() + '\n'
+ 'Some Custom Variable Information From Class: ' + myClassVariable;
insert log;
}
Because each error creates a new Error_Log__c record, manage data volume over time by creating a scheduled Batch Apex class that runs monthly and deletes Error_Log__c records older than 30 days. This prevents unnecessary data accumulation in your org.
This method is flexible — you can include different contextual variables from each class depending on what is most useful for debugging that specific job. It scales well across multiple scheduled classes, even those running nightly, because you only receive alerts when something goes wrong. The approach requires no external tools and is fully contained within the Salesforce platform.
000387981

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.