2

This Firing Platform Events from Batch Apex documentation largely discusses automatically publishing a BatchApexErrorEvent on an unhandled exception:

If the start, execute, or finish method of a batch Apex job encounters an unhandled exception, a BatchApexErrorEvent platform event is fired. For more details, see BatchApexErrorEvent.

if the class implements Database.RaisesPlatformEvents. So at first sight, this is a switch to turn on the automatic conversion of exceptions to the BatchApexErrorEvent Platform Event in Batchables to improve the error reporting.

My question is whether this interface also needs to be implemented to allow other Platform Events to be explicitly published via an EventBus.publish call? This is a different scenario not discussed in the linked documentation.

(A precedent here is that to make callouts you must implement Database.AllowsCallouts.)

Keith C
  • 135,775
  • 26
  • 201
  • 437

2 Answers2

4

There is no restriction preventing EventBus.publish from a Database.Batchable. You can publish platform events without implementing Database.RaisePlatformEvents. The only effect of this interface is that it automatically publishes BatchApexErrorEvent records in the case of an unhanded exception.

Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
0

It's a switch to turn it on. You could try it by throwing your own exception in a batch class.

Theodoor
  • 451
  • 1
  • 4
  • 15
  • Thanks Theodoor. My batchable calls some existing code that does its own EventBus.publish. So I'm looking for confirmation that Database.RaisesPlatformEvents is required for that case as well as the automatic unhandled exception case. – Keith C Mar 04 '21 at 18:34
  • 1
    As Adrian stated, it's not needed to publish your own events. It's just a switch to automatically get the events when it fails. – Theodoor Mar 08 '21 at 08:41