0

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.

  • 1
    We would need to see the code you've written. – sfdcfox Aug 1 '18 at 15:21
  • I added the code to the question. Thanks for the reply! – Lukas Aug 1 '18 at 15:31
0

You are performing the request send before you populate the endpoint or any of the parameters.

        HttpRequest req = new HttpRequest();
        String endpoint = 'https://myendpoint.com/';

        HttpResponse response = http.send(req);

At that line above, the request's endpoint is null (and so is everything else). Don't send the request until after you complete populating its body, endpoint, and parameters.

  • You're a star! Salesforce support didn't pick up on this! Thanks a lot David. – Lukas Aug 1 '18 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.