I have a rest resource (developed by a vendor) that I am calling in my batch apex class that posts some data. The code is as below and works in partial and dev sandboxes but not in full sandbox as the UserInfo.getSessionID() return null there by failing my batch apex class. I am wondering is there any other way of getting around it or would I need to add a connected app with in the same org and access it via consumer key and secret (seems a bit daft to be honest)?
String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm();
String restAPIURL = sfdcURL + '/services/apexrest/***/**/**/**';
HttpRequest httpRequest = new HttpRequest();
httpRequest.setEndpoint(restAPIURL);
httpRequest.setMethod('POST');
httprequest.setHeader('Content-Type', 'application/json');
httprequest.setHeader('Accept','application/json');
httpRequest.setHeader('Authorization', 'OAuth '+UserInfo.getSessionID());
httpRequest.setHeader('Authorization', 'Bearer '+UserInfo.getSessionID());
httpRequest.setBody(json.serializePretty(PayLoad));
System.debug('UserInfo.getSessionId() '+UserInfo.getSessionId());
String response = '';
try {
Http http = new Http();
HttpResponse httpResponse = http.send(httpRequest);
System.debug('>> Response of payload >> '+httpResponse.getStatusCode());
response = 'Status Code: '+httpResponse.getStatusCode()+' Response: '+httpResponse.getBody();
} catch(Exception e) {
System.debug('ERROR: '+ e.getMessage());
response = e.getMessage();
sendEmail('Payload Sending to Kimble failed','Exception Occurred Payload Push',e.getMessage(),true,restAPIURL);
}
System.debug(' ** response ** : ' + response );