Get Hands-on Using Platform Events from Batch Apex Classes

Requirement : Modify an existing batch Apex job to raise BatchApexErrorEvents

Take an existing batch Apex job class and update it to implement the Database.RaisesPlatformEvents interface. Then, add a trigger on BatchApexErrorEvent that logs exceptions in the batch job to a custom object.

  • Update the BatchLeadConvert class to implement the Database.RaisesPlatformEvents marker interface.
  • Create an Apex trigger called BatchApexErrorTrigger on the BatchApexErrorEvent SObject type. For each event record, capture the following fields and save them to the corresponding fields in a new BatchLeadConvertErrors__c record.
    • AsyncApexJobId: AsyncApexJobId__c
    • JobScope: Records__c
    • StackTrace: StackTrace__c
  • To make the trigger bulk safe, use a single DML statement to insert a list of new records at the end.

Solution

8 Thoughts to “Get Hands-on Using Platform Events from Batch Apex Classes”

  1. xSo

    Hello,

    What is the BatchLeadConvert test class please? :/

    Thanks

    1. Please follow the trail and Install Appexchsnge package, then make these changes.
      The appexchange package contains the test classes.

      1. xSo

        It’s working. Thank you so much for your job ! Have a good day

        1. Enzo

          How did you resolve the test class? I installed the package, updated the Apex Class but I still have issues with my test.

          1. What error you’re getting?

  2. Deepak Konjety

    I am getting the error :
    When executed, the BatchLeadConvertTest test class has test failures. All tests should pass.

    1. Can you share the Error/Screenshot on RUN TEST for BatchLeadConvertTest Class in Dev Console.

    2. Make sure you have added BatchApexErrorTrigger class.

Leave a Comment