Find your content:

Search form

You are here

Tracking a bug while running batch job


I have 2000 batches to run in a single batch job. One of the batch gives "Attempt to de-reference a null object" error. I want to track this error. How can I track? I have used debug logs but they are pretty much in quantity. Is there any way to do so?

Attribution to: doga

Possible Suggestion/Solution #1

I would suggest that you need to add some additional error handling to your batch job. This error message will be caused by you trying to reference a variable that is null, you would need to defensively code for this.

One thing you can do, is to add the Database.Stateful interface to your batch apex class method declaration, e.g.

global class InvoicePosterBatch implements Database.Batchable<SObject>, Database.Stateful

By adding this, you can maintain some kind of log of what is happening - you have to be careful though not to exceed the heap size. One strategy I sometimes employ for circumventing that is something like this:

    if( Limits.getHeapSize()*1.1 >= Limits.getLimitHeapSize() )
        failures = new List<String>();

Debugging the batch 'chunks' is difficult because not only do you have your chunk log, you then may have up to 2000 iterations of records within that log.

Another thing you could do, is say reduce your batch size to 1 and then you will see which record is failing, you could do that by querying the AsyncApexJob object, e.g.

Select Status, NumberOfErrors, ExtendedStatus 
From AsyncApexJob
Where ParentJobId = '707U0000008qTH7'
and NumberOfErrors > 0

You can retrieve the 'ParentJobId' from Setup->Monitor->Apex Jobs

Attribution to: Phil Hawthorn

Possible Suggestion/Solution #2

If you had debug logging enabled - just query the table that contains them:

select Id, Application, DurationMilliseconds, LastModifiedDate, Location, LogLength, LogUserId, Operation, Request, StartTime, Status, SystemModstamp 
from ApexLog
where status = 'Attempt to de-reference a null object'

(of course fine-tune the query to your user, time criteria etc as you see fit)

Then you can view the log by visiting link like that:

(again - provide your own SF instance and log_id)

EDIT (to answer the comment)

Yeah, I'm positive Status-based search can be the way to dig it out.

select Count(Id), Status
from ApexLog
group by status
order by Count(Id) DESC

results from my prod. org.

Attribution to: eyescream

Possible Suggestion/Solution #3

Log all the error messages in a custom object .This is the best method i have heard so far.Keep the batch Id and record Id that failed and the error message in the custom object for each batch run.You can clean this for every month to avoid huge data in this .

Attribution to: Mohith Shrivastava
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content