[SalesForce] How to setup debug log for batch apex

I have a batch 'mytestbatch' which runs everyday through scheduler. This batch will update more than 15lakhs records. During execution mytestbatch is throwing an error 'First error: Attempt to de-reference a null object'. I know this error is coming might be due to some bad data or some null values during an execution. I need to know from which line this error is coming. Under setup I have set the debug log for an user on whose name mytestbatch is running. But not able to capture the error due to exceeding debug log limits as more records are getting updated in this batch.
Can anyone tell how to capture this root cause of this error?

Best Answer

There are basically two options open to you:

  1. Ensure that you set up a Debug Levels definition that minimizes most of the captured data, as covered in the documentation, whilst definitely capturing at least ERROR level for Apex. Indeed, for the issue you face you can probably set everything to just ERROR. Once you set up the debug levels, use it on the trace flags you create for the batch's execution user.
  2. If you need more complete information you can still have more captured data if you can change the batch's scope chunk size, e.g. to 1. This means you will get a separate debug log per record processed by your batch. Depending how you wrote the code that calls Database.executeBatch this may require a small code change.

It may be that you could manually invoke the batch through anonymous apex, in which case you can simply set up the SFDC_Console debug level, open the developer console, access the Execute Anonymous Apex dialog and explicitly call the execute batch. The console debug level is automatically applied to the user you opened the developer console as.

Related Topic