Newer Version Available

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

Apex, API, and SOQL Considerations for Platform Events

Be familiar with the considerations when publishing and subscribing to platform events.
Only after insert Triggers Are Supported
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 daily event 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
If you create Salesforce records in platform event triggers, set the ownerId field in those records 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    AccountId = acc.Id, 
3    StageName = 'Qualification', 
4    Name = 'A ' + customerOrder.Product_Name__c + ' opportunity for ' + acc.name, 
5    CloseDate = Date.today().addDays(7), 
6    OwnerId = customerOrder.createdById);
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.
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 user on the Debug Logs page in Setup. To collect logs in the Debug Logs page for your Apex code that publishes the events, add another trace flag entry for your user.
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 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 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.
No SOQL Support
You can’t query event notifications using SOQL.