0

We have two batch jobs that run just after midnight. When I run it manually via the Developer Console Anonymous window, they run fine. But last night, the "Apex Jobs" window showed that they failed. The Status states:

 Completed  First error: Attempt to de-reference a null object

Now I know what that means, but I have no information to debug it. As I said, when I run it manually I can turn on the debug logs, but it does not fail.

It seems odd that there is no "master error log" where I can see more about what happened... or is there?

2

I'm afraid that you would need to have the Debug Logs running on the user who scheduled the actual batch job, as well as informative debug code (e.g. System.debug) within your Scheduled Apex class. If you to Setup | Monitor | Logs, you can add the user record which should be monitored. Resulting in different debug logs.

Note: only 20 logs can be created. So in case you have batch job scheduled during midnight, set up the user for logging right before leaving the office (hereby ensuring the user running the batch will not browser anymore and hereby not consume any of the 20 logs that can be created). Check again in the morning on the same place, you should be able to find back the logs from the batch.

1

In addition to @sfdcfox's suggestion, you might want to consider

a) wrapping the batch start(), execute(), and finish() methods with try-catch.

b) in the catch block, rollback and then write the exception to a custom object AsyncLog__c - that can also be used to capture interesting events logged to a stateful String variable in the batch class

In lieu of b), you could also send an email to the sysad with stateful trace data you manage in the batch class as well as the exception + stacktrace

Note that this approach won't work if the batch fails with a limit exception as these can't be caught.

  • There are try-catch blocks around the DML operations, and one to catch any Callout exception, but not around everything. And there is a custom object called Feed_Log that is used to try and capture everything, but this is falling through the cracks. I was able to get a log by turning on the log for last night's run, so I think I at least have the information to debug now. – mitchmcc Mar 28 '15 at 13:18
  • right -- as you saw, you got a null pointer de-reference exception which would not occur on DML hence my suggestion for try-catch around the entire execute() . Best wishes towards successfully debugging this. – cropredy Mar 29 '15 at 4:49
0

Login as the user the batch runs as, and open the developer console. All batch executions will appear in the console's log tab. You may need to elevate the user's permissions while you test the code. Also, consider writing unit tests for your batch. That is how you'd find problems usually, by having test methods that thoroughly test your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.