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?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
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.
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:
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.
You can retrieve the 'ParentJobId' from Setup->Monitor->Apex Jobs
If you had debug logging enabled - just query the table that contains them:
(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: https://cs7.salesforce.com/p/setup/layout/ApexDebugLogDetailEdit/d?setupid=ApexDebugLogs&apex_log_id=07LM0000008VWd3MAG
(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.
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 .