0

Formerly, I built a callout using the @future(callout=true) to make asynchronous callouts.

@future(callout=true)
public static void exportReport(){

        // create request
        Http http = new Http();
        HttpRequest request = new HttpRequest();

        request.setMethod('POST');
        request.setEndpoint('callout:Expensify_API');    
        request.setHeader('Content-Type', 'application/x-www-form-urlencoded'); 
        request.setBody('requestJobDescription={"type":"file","credentials":{"partnerUserID":"{!$Credential.Username}","partnerUserSecret":"{!$Credential.Password}"},"onReceive":{"immediateResponse":["returnRandomFileName"]},"inputSettings":{"type":"combinedReportData", "employeeEmail":"'+employeeEmail+'","filters":{"reportIDList":"'+expensifyReportID+'"}},"outputSettings":{"fileExtension":"csv"}}');

        HttpResponse response = http.send(request);

        if (response.getStatusCode() == 200) {
            System.debug(response.getBody());
        }
        else {
            System.debug(response.getBody());
        }
    }

This snippet is working code and doing what I expect. Now I learned that I should use the Continuation class if I want to use Apex buttons. So I rewrote the code to the following:

 public Object exportReport(){

        // create continuation with timeout
        Continuation con = new Continuation(60);
        // set callback method
        con.ContinuationMethod = 'processExportResponse';

        // create request
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setMethod('POST');
        request.setEndpoint('callout:Expensify_API');    
        request.setHeader('Content-Type', 'application/x-www-form-urlencoded'); 
        request.setBody('requestJobDescription={"type":"file","credentials":{"partnerUserID":"{!$Credential.Username}","partnerUserSecret":"{!$Credential.Password}"},"onReceive":{"immediateResponse":["returnRandomFileName"]},"inputSettings":{"type":"combinedReportData", "employeeEmail":"'+employeeEmail+'","filters":{"reportIDList":"'+expensifyReportID+'"}},"outputSettings":{"fileExtension":"csv"}}');

        System.debug(request.getBody());
        // add callout request to continuation
        this.exportR_requestLabel = con.addHttpRequest(request);

        // return continuation object
        return con;
    }

    // callback method from export report
    public Object processExportResponse (){
        // get response from unique continuation label
        HttpResponse response = Continuation.getResponse(this.exportR_requestLabel);
        // set output to be displayed on Visualforce page
        this.exportR_result = response.getBody();

        // return null to re-render original Visualforce page
        return null;
    }

and I receive an error message from the API that Authentification failed:

{"responseMessage":"Authentication error","responseCode":402}

Has anyone encountered that problem? Do I have to set the request body differently when using continuations?

Many thanks in advance.

  • 1
    Check with your service provider. 402 is defined as Payment Required, and may indicate a billing problem with your account. – sfdcfox Apr 17 at 13:04
  • Could you have a guess why I receive this only using continuation and not future method? Cause I checked the future method again and it still works fine – steph Apr 17 at 14:51

Your Answer

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

Browse other questions tagged or ask your own question.