0

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?

0

You can send email in execute method like this,

whenever there is any error it will send email :

global void execute(Database.BatchableContext bc, List<sObject> scope) {

    try{
       //process data here
    }
    catch(Exception e) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'your email address'};
        mail.setToAddresses(toAddresses);
        mail.setSubject('Error in mytestbatch ' );
        //You can add more detail about error from Exception object
        mail.setPlainTextBody(e.getStackTraceString());
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }

}
1
  • Thanks Kritik. I have this batch class schdueled in Prod So only I'm not able to incorporate any email sending methods or automatic case generations for error. – Vignesh S Nov 4 '20 at 9:30
0

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.

3
  • Thanks Phil. I will do what you said and let you know as this batch runs daily for more time. So I need one day to test this. – Vignesh S Nov 4 '20 at 9:32
  • Like I said, you could run it manually through the developer console, assuming this is easy to construct an instance of the batch... – Phil W Nov 4 '20 at 9:34
  • Yep but this batch is already scheduled in Prod. So I don't want to disturb that. Let it run in scheduleded time – Vignesh S Nov 4 '20 at 9:37
0

I got the required error log through..... I have created a new debug levelenter image description here (Apex code,system,database Finest and other are None)and created a debug log for an user on whose name the batch is running. As soon as batch starts runs then parallelly I keep deleting the success logs. In this I'm not getting hitted with any limits. I know its quite difficult due to continuous monitor and human effort. But when we are failing to implement error capture method then we have to suffer like this only. Good to keep error capture method in all classes.

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.