I struggle with execution of HttpRequest POST in my Scheduled Batch job. I have written Test and Mock classes to test the callout itself (without Batch job) and it returns http code 200. My debug commands show the json file body is as it's supposed to and the endpoint is also good.
However, when I try to execute Scheduled Batch job it always throws "Endpoint can not be null" exception while endpoint is clearly stated in a code (exactly the same way as in only callout class. Did anyone had similar experience? Do i need to declare endpoint someplace else than in execute()?
EDIT: Missing code from Batchable class.
global class PickedShippedUpdateRequest implements Database.Batchable<sObject>, Database.AllowsCallouts {
global Database.QueryLocator start (Database.BatchableContext BC){
return Database.getQueryLocator([SELECT Sales_Order_Number__c FROM Case WHERE Sales_Order_Number__c != null AND Status = 'Approved']);
}
global void execute (Database.BatchableContext BC, List<Case> scope){
List <Case> salesordernumbers = new List<Case>();
for (Case cases: scope){
Http http = new Http();
HttpRequest req = new HttpRequest();
String endpoint = 'https://myendpoint.com/';
HttpResponse response = http.send(req);
String body = '{';
body += ' "SalesOrderfromOpenClaim": {';
body += ' "SONUMBER":"' + cases.Sales_Order_Number__c + '"}}';
req.setClientCertificateName('Certificate');
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setBody(body);
req.setCompressed(true);
System.debug(body);
System.debug('This is enpoint' + endpoint);
System.debug('cases' + cases);
}
}
global void finish (Database.BatchableContext BC){
}
}
I've changed endpoint and client certificate because of security concerns.