Newer Version Available
Considerations for Publishing and Subscribing to Platform Events with Apex and API
Before you use Apex or Salesforce APIs to publish and subscribe to
platform events, familiarize yourself with these considerations.
- Support Only for after insert Triggers
- Only after insert triggers are supported for platform events because event notifications can’t be updated. They’re only inserted (published).
- Infinite Trigger Loop and Limits
- Be careful when publishing events from triggers because you could get into an infinite trigger loop and exceed limits. For example, if you publish an event from a trigger that’s associated with the same event object, the trigger is fired in an infinite loop.
- Apex DML Limits for Publishing Events
- Each EventBus.publish method call is considered a DML statement, and DML limits apply.
- Platform Event Triggers: ownerId Fields of New Records
- In platform event triggers, if you create a Salesforce record that contains an ownerId field, set the ownerId field explicitly to the appropriate user. Platform event triggers
run under the Automated Process entity. If you don’t set the ownerId field on records that contain this field, the system sets the
default value of Automated Process. This example
explicitly populates the ownerId field for an
opportunity with an ID obtained from another
record.
1Opportunity newOpp = new Opportunity( 2 OwnerId = customerOrder.createdById, 3 AccountId = acc.Id, 4 StageName = 'Qualification', 5 Name = 'A ' + customerOrder.Product_Name__c + ' opportunity for ' + acc.name, 6 CloseDate = Date.today().addDays(7)); - For cases and leads, you can alternatively use assignment rules for setting the owner. See AssignmentRuleHeader for the SOAP API or Setting DML Options for Apex.
- API Request Limits for Publishing Events
- Because platform events are published by inserting the event sObjects, API request limits apply. For more information, see API Request Limits and Allocations in the Salesforce Limits Quick Reference Guide.
- Replaying Past Events
- You can replay platform events that were sent in the past 24 hours. You can replay platform events through the API (CometD) but not Apex. The process of replaying platform events is the same as for other Streaming API events. For more information, see the following resources.
- Filtered Subscriptions
- Filtered subscriptions in Streaming API aren’t supported for platform events.
- Publishing Events in Read-Only Mode
- During read-only mode, publishing platform events results in an exception and the events aren't published. In contrast, you can publish high-volume platform events in read-only mode. Your org is in read-only mode during Salesforce maintenance activities.