Loading

Set Up Email Alerts for Errors in Scheduled Apex Jobs

게시 일자: Jun 11, 2026
상세 설명

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.

솔루션

Overview of the Error Notification Approach

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.

Step 1: Create an Error Log Custom Object

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.

Step 2: Capture Errors in Your Apex Class

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:

  • Exception type: use e.getTypeName()
  • Exception cause: use e.getCause()
  • Error message: use e.getMessage()
  • Line number: use e.getLineNumber()
  • Full stack trace: use 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.

Step 3: Create a Workflow Rule to Send Email Alerts

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;         
}

Maintenance Consideration: Clean Up Old Error Logs

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.

Advantages of This Approach

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.

Knowledge 기사 번호

000387981

 
로드 중
Salesforce Help | Article