9

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!

  • 5
    This is a very well asked question, even moreso for a first question. Hope you feel like sticking around and contributing more questions (and answers) in the future! – Derek F Apr 26 at 14:51
9

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.

  • 1
    Thank you Jayant for your good answer, good to know those are the only 2 reasons a platform event might fail. Regarding 1) the hourly publishing limit of 100K for enterprise editions, this should probably best be accounted for in the solution design and 2) it's good to be able to know that it failed and why, so you can act on it if an event can't be published. – Dirk May 1 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.