0

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));
}

1 Answer 1

0

You can do the following (assuming theResult is a stateful batch class variable)

RestContext.response resp = new RestResponse();
resp.responseBody = Blob.valueOf(JSON.serialize(theResult)); 

Note it is a bit weird for the finish() to construct a RestResponse but presumably you are passing this to some other method that is expecting this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .