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

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

    1. Can you please show me the error of insert failed.
      As Line 28 is an insertion for a Lead record, this shouldn’t be a problem.

  3. Alice Lin

    I got “The ‘BatchApexErrorTrigger’ could not be found”

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

  4. I was looking through some of your articles on this site and I conceive this website is rattling instructive! Retain putting up.

  5. I think this is one of the most vital information for me. And i’m glad reading your article. But want to remark on some general things, The site style is perfect, the articles is really great : D. Good job, cheers

  6. Siva

    I got error whenever i execute the challenge No Apex class named ‘BatchLeadConvert’ was found. Refer to the setup instructions and install the unmanaged package.

    1. Make sure you’ve installed App-exchange package mentioned in trail before making these changes.

  7. Siva

    You are right, I was not able to install that in Trailhead Playground. I was able to install that one in Developer Org only.
    S

    1. Sounds great. Thank you Siva for the update.

  8. Annappa

    iam not able to install the package in my developer account.How to ressolve this error:

    This app can’t be installed.
    There are problems that prevent this package from being installed.
    Illegal assignment from List to Apexclass BatchLeadConvertTest: Illegal assignment from List to Apexclass
    Variable does not exist: body BatchLeadConvertTest: Variable does not exist: body

    1. Hi,

      Make sure you’ve copied an pasted code correctly.
      Plus, Install the appexchange package first before these changes.

Leave a Comment