Newer Version Available

This content describes an older version of this product. View Latest

Subscribe to Platform Event Notifications with Apex Triggers

Use Apex triggers to subscribe to events. You can receive event notifications in triggers regardless of how they were published—through Apex or APIs. Triggers provide an autosubscription mechanism. No need to explicitly create and listen to a channel in Apex.

To subscribe to event notifications, write an after insert trigger on the event object type. The after insert trigger event corresponds to the time after a platform event is published. After an event message is published, the after insert trigger is fired.

This example shows a trigger for the Low Ink event. It iterates through each event and checks the Printer_Model__c field value. The trigger inspects each received notification and gets the printer model from the notification. If the printer model matches a certain value, other business logic is executed. For example, the trigger creates a case to order a new cartridge for this printer model.

An Apex trigger processes platform event notifications sequentially in the order they’re received. The order of events is based on the event replay ID. An Apex trigger can receive a batch of events at once. The order of events is preserved within each batch. The events in a batch can originate from one or more publishers.

Unlike triggers on standard or custom objects, triggers on platform events don’t execute in the same Apex transaction as the one that published the event. The trigger runs asynchronously in its own process under the Automated Process entity. As a result, there might be a delay between when an event is published and when the trigger processes the event. Also, debug logs corresponding to the trigger execution are created by Automated Process. System fields, such as CreatedById and LastModifiedById, reference the Automated Process entity.

If you create a Salesforce record with an ownerId field in the trigger, such as a case or opportunity, explicitly set the owner ID. For cases and leads, you can alternatively use assignment rules to set the owner. See Apex, API, and SOQL Considerations for Platform Events.

Note

Event triggers have many of the same limitations of custom and standard object triggers. For example, you can’t make Apex callouts from triggers with some exceptions. For more information, see Implementation Considerations for Triggers in the Apex Developer Guide. Also, the trigger is subject to Apex governor limits.

Debug Logs for Platform Event Triggers

Debug logs for platform event triggers are created by a process called “Automated Process” and are separate from their corresponding Apex code logs. The debug logs aren’t available in the Developer Console’s Log tab. One exception is Apex tests, which include debug logging for event triggers in the same test execution log. To collect platform event trigger logs, add a trace flag entry for the Automated Process entity in Setup.

  1. From Setup, enter Debug Logs in the Quick Find box, then click Debug Logs.
  2. Click New.
  3. For Traced Entity Type, select Automated Process.
  4. Select the time period to collect logs and the debug level.
  5. Click Save.

To collect logs in the Debug Logs page for your Apex code that publishes the events, add another trace flag entry for your user.

For more information, see Set Up Debug Logging in the Salesforce Help.