I am creating a REST class wherein all the processing is done by a batch class. I would want the RestContext.response line to be made after the execute method. But it returns this error: "System.NullPointerException: Attempt to de-reference a null object". This error occurs even if I put the line of code in the execute method.
Is it possible to put the RestContext.response in a batch class? Or is there any better way to do it? Hope you can help me...
This is my code:
global class sample_API {
@HttpPost
global static void getAccounts() {
sampleAPIBatch runAPIBatch = new sampleAPIBatch();
Id batchJobId = Database.executeBatch(runAPIBatch);
}
}
/batch class/
global class sampleAPIBatch implements Database.Batchable <sObject>, Database.Stateful, Database.AllowsCallouts {
...
global void execute(Database.BatchableContext bc, List<Account> scope) {
...
}
global void finish(Database.BatchableContext bc) {
//here results to : System.NullPointerException: Attempt to de-reference a null object
RestContext.response.responseBody = Blob.valueOf(JSON.serialize(theResult));
}