TransWikia.com

How to get to the Id of batchableContext.getJobId() when handling a BatchApexErrorEvent?

Salesforce Asked by Peter Knolle on November 28, 2021

I have a batch job that implements Database.RaisesPlatformEvents and when a BatchApexErrorEvent is generated from the Batch class’s execute() method I handle it in a BatchApexErrorEventTrigger. I would like to relate the BatchApexErrorEvent back to the Batch’s job, but I’ve found:

  • batchableContext.getJobId() != event.AsyncApexJobId. <– how do I get this JobId from event???
  • batchableContext.getChildJobId() == event.AsyncApexJobId

The scenario is that I create a custom log record in the start method and set the batchableContext.getJobId() on it. If there is an error in the execute method (could be uncatchable), the BatchApexErrorEventTrigger handles it and from there I need to relate back to that initial log record. I cannot, reliably, create and/or update the DB in the execute method to supply a child ID since an error could occur at any time in the execute() and an exception could roll back anyway.

Is there a way to get to the Id of batchableContext.getJobId() when handling a BatchApexErrorEvent?

I thought that the event.RequestId would work, but that ID is not a job Id. When I write it out it is writing records that start with 4XF. 4XFToiUSPaAR3ykCac.

One Answer

If you are getting back the child job ID, why don't you obtain the parent job ID this way:

SELECT ParentJobId FROM AsyncApexJob WHERE Id = :event.AsyncApexJobId

(Clearly you would want to actually bulkify this in real life)

Answered by Phil W on November 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP