Newer Version Available

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

Considerations for Publishing and Subscribing to Platform Events with Apex and APIs

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.

In rare occasions, some Salesforce maintenance activities, such an org migration to a new data center, might wipe retained high-volume platform events.

Note

Filtered Subscriptions
Filtered subscriptions in Streaming API aren’t supported for platform events.
Millisecond Time Precision in DateTime Fields
For event messages delivered to CometD clients in JSON format, the DateTime fields include the number of milliseconds. The date format, which is in the ISO 8601 standard, is: YYYY-MM-DDTHH:mm:ss.sssZ. In API version 42.0 and earlier, DateTime fields don’t include the millisecond part of the time, and the DateTime format is YYYY-MM-DDTHH:mm:ssZ.
For event messages delivered to Apex triggers, DateTime fields don’t include millisecond precision, like DateTime fields of Salesforce objects.