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.)

2 Answers 2

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.

5
  • This post is based on previous experience, just checking with the latest API Versions to confirm.
    – Adrian Larson
    Commented Mar 4, 2021 at 19:25
  • Makes sense as the two cases have differing purposes. Do you know if you lose anything e.g. Apex Jobs logging by turning this on?
    – Keith C
    Commented Mar 4, 2021 at 19:40
  • 1
    The jobs still get logged normally. I haven't found any loss based on implementing this interface.
    – Adrian Larson
    Commented Mar 4, 2021 at 20:05
  • @AdrianLarson Do you know if REQUEST_RUNNING_TOO_LONG qualifies for BatchApexErrorEvent?
    – MandyKool
    Commented Sep 21, 2023 at 4:16
  • I don't know, no.
    – Adrian Larson
    Commented Sep 21, 2023 at 14:38
0

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

2
  • 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
    Commented Mar 4, 2021 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
    Commented Mar 8, 2021 at 8:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .