[SalesForce] What causes platform events to fail to be published and should I cater for failed platform event creations

After examining docmentation I have began to create a platform event from Apex, but I have 3 unanswered questions when it comes to the creation of platform events concerning unsuccessfull publishing of a platform event.

For creation of a platform event, you can use the EventBus Class and the publish(events) method – which returns a database.saveresult. "The EventBus.publish() method doesn’t throw exceptions caused by an unsuccessful publish operation". Based on this statement, one might wonder:

  • Because this is a database.saveresult, which can return a boolean 'saveResult.isSuccess', you can suspect that the creation of a platform event could also fail. What could cause a creation of a platform event to fail?
  • The saveresult is by default 'all or nothing', analogue to a database.Saveresult: "The EventBus.publish() method doesn’t throw exceptions caused by an unsuccessful publish operation. It is similar in behavior to the Apex Database.insertmethod when called with the partial success option." Why would this be an 'all or nothing'? To me it doesn't make sense, since you'd always want all platform events to succeed and if they do fail, I want an exception or at least I want to know which events failed to be created and why.

Should I cater for the failing of the creation of platform events in my code? Or is this most likely to never happen? I could throw an exception or show the situations (records in this case) for which the event creations failed to an admin.
Thanks in advance for your help!

Best Answer

What could cause a creation of a platform event to fail?

You can actually find this on Platform Event Error Status Codes which lists down the reasons for the failures. And it seems there are primarily only two of such scenarios where it could fail.

LIMIT_EXCEEDED

The number of published platform event messages exceeded the hourly publishing limit or the test limit for event messages published from an Apex test context.

PLATFORM_EVENT_PUBLISHING_UNAVAILABLE

Publishing platform event messages failed due to a service being temporarily unavailable. Try again later.

...

The status code can be returned in Apex in the Database.SaveResult in the Database.Error object


For your other question:

I want an exception or at least I want to know which events failed to be created and why

The Event.publish() does return if a publish failed:

The result of publishing the given event. Database.SaveResult contains information about whether the operation was successful and the errors encountered. .... If isSuccess() returns false, the event publish operation resulted in errors, which are returned in the Database.Error object.

This is also mentioned in the status code docs as referred above.


As for handling the exceptions, I think as long as you do not really hit the aforementioned limits or platform issues, you should be fine. But still if you want to, you do have a way to know if the publishing failed or succeeded.

Related Topic