Loading

Setting Up Data Cloud Search Index Failure Notifications via Apex & Platform Events

Veröffentlichungsdatum: Aug 11, 2026
Beschreibung

Prerequisites & Tools Before beginning, ensure you have installed:

Salesforce CLI

Visual Studio Code (VS Code)

Open VS Code > Extensions > Salesforce Extension Pack (Installed via VS Code Extensions tab: Ctrl+Shift+X or Cmd+Shift+X)

Important Note on Deployment: This Apex Trigger must be created and deployed using VS Code / Salesforce CLI. Salesforce does not support creating Apex Triggers with the after insert event on SearchIndexJobStatusEvent directly within the Developer Console UI. This Platform event is currently not subscribable within Platform Event Trigger Flow.  

Step 1: Set Up Your Project & Authorize Org

  1. Open VS Code.
  2. Open the Command Palette (Cmd + Shift + P on Mac / Ctrl + Shift + P on Windows).
  3. Select SFDX: Create Project.
    1. Enter your Project Name.
    2. Select your target Folder Location.
  4. Open the Command Palette again and select SFDX: Authorize an Org.
    1. Choose Default Login.
    2. Log in to your Target Org / SDO using your credentials.
    3. Select Yes when prompted to allow access.

Step 2: Create the Apex Trigger & Metadata Config

  1. Create the Apex Trigger:
    1. Command Palette > SFDX: Create Apex Trigger.
    2. Name the trigger SearchIndexJobStatusTrigger and accept the default folder location.
    3. Paste the following code into the newly created file:
trigger SearchIndexJobStatusTrigger on SearchIndexJobStatusEvent (after insert) {
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

    for (SearchIndexJobStatusEvent evt : Trigger.new) {
        if (evt.RuntimeStatus == 'FAILED') {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            // Set recipients - update with your admin email(s)
            mail.setToAddresses(new List<String>{ '<set_your_admin_email>' });

            // Set your Email Subject Line
            mail.setSubject('⚠️ Search Index FAILED: ' + evt.ApiName);

            // Create your email Body message
            String body = 'A Data Cloud Search Index job has FAILED.\n\n'
                + 'Index API Name: ' + evt.ApiName + '\n'
                + 'Status: ' + evt.RuntimeStatus + '\n'
                + 'Error Code: ' + evt.ErrorCode + '\n'
                + 'Event Created: ' + evt.EventCreationDate + '\n'
                + 'Index Refreshed On: ' + evt.IndexRefreshedOn + '\n\n'
                + 'Please investigate the Search Index in Data Cloud.';

            mail.setPlainTextBody(body);
            mail.setSaveAsActivity(false);
            emails.add(mail);
        }
    }
    
    // Check if the emails object has mail - If not empty, send immediately
    if (!emails.isEmpty()) {
        Messaging.sendEmail(emails);
    }
}

SearchIndexJobStatusEvent fields:

Fields

Data Type

Description

ApiNamestringThe search index API name.
ErrorCodestringReference code for the type of error that occurred.
EventCreationDatedatetimeThe date and time when the event occurred.
EventPublishDatedatetimeThe date and time when the event was published.
EventTypestringThe type of event that occurred.
EventUuidstringA universally unique identifier (UUID) that identifies a platform event message.
IndexRefreshedOndatetimeThe date and time of the last refresh on the index.
ReplayIdstringRepresents an ID value that is populated by the system and refers to the position of the event in the event stream. Replay ID values aren’t guaranteed to be contiguous for consecutive events. A subscriber can store a replay ID value and use it on resubscription to retrieve missed events that are within the retention window.
RuntimeStatusstringThe last run status of the search index such as Ready, In Progress, or Failed.
SchemaVersionstringThe version of the event schema.
  1. Create Platform Event Subscriber Config:
    1. In the same folder as your Apex Trigger, create a new file name: SearchIndexJobStatusConfig.platformEventSubscriberConfig-meta.xml
    2. Copy the XML template below into this file and update the highlighted tags:
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventSubscriberConfig xmlns="http://soap.sforce.com/2006/04/metadata">
    <platformEventConsumer>SearchIndexJobStatusTrigger</platformEventConsumer>
    <!-- Name of the apex trigger -->
    <batchSize>500</batchSize>
    <masterLabel>SearchIndexJobStatusTriggerConfig</masterLabel>
    <user>dc_tommy.mac@salesforce.com</user> 
    <!-- username of the user running this trigger. If the user is deactivated, it could cause silent issue. Be sure to create a dedicated admin user to run this trigger. -->
    <isProtected>false</isProtected>
</PlatformEventSubscriberConfig>

Step 3: Deploy to Source Org

  1. In the VS Code Explorer, right-click the default folder (under force-app/main/default).
  2. Select SFDX: Deploy Source to Org.
  3. Wait for the success notification in the terminal.

Step 4: Verify & Test in Salesforce

  1. Verifying the Deployment
    Log in to your Salesforce Org.
  2. Navigate to Setup > Developer Console.
  3. Go to File > Open (or Ctrl + O / Cmd + O).
  4. Select Triggers, search for SearchIndexJobStatusTrigger, and double-click to view.

Testing the Setup

  1. Trigger a rebuild on your search index.
  2. Verify that an alert email is sent whenever the RuntimeStatus returns FAILED (or change FAILED to SUBMITTED in the trigger temporarily if testing initial execution in a sandbox environment).
Nummer des Knowledge-Artikels

005390905

 
Laden
Salesforce Help | Article