We have private field service
which is instance of HelperService
. HelperService
is pretty heavy class, that performs several callouts, transforms responses into Salesforce objects and logs errors which will be used for email messaging in the batch's finish
method.
Based on query generation logic in the start
method, batch either enters execute
method and fails because of NullPoiner, or do not receive records for processing and proceeds to finish
method where it fails with the same NullPoiner exception. service
field appears to be null, even though we have instantiated it.
As seen on piece of code below service
field is not static, and Database.Stateful
interface has been implemented which means instance of HelperService
should be serialized between chunks. This issue is not reproducible on sandbox, occur only in production. Also it fails only when ran via scheduler, manual launch with anonymous apex worked as expected, issue did not occur.
Have someone faced such behavior, if so - is there any possible workarounds? Thanks everyone in advance.
public class BatchClass implements Database.Batchable<SObject>, Database.Stateful {
private final HelperService service = new HelperService();
public Database.QueryLocator start(Database.BatchableContext bc) {
//Start logic...
}
public void execute(Database.BatchableContext bc, List<SObject> records) {
service.setBar(...) //Here NullPointer exception occurrs
}
public void finish(Database.BatchableContext bc) {
service.getFoo() //Here NullPointer exception occurrs
}
}