function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Turner NelsonTurner Nelson 

How can I wait for an Http Response inside a future?

I currently have a problem involving triggers, futures, callouts, and HttpResponses.

Currently, when my trigger is called, it calls a future function, which contains a callout to make a POST Http Request. This POST request is expecting a Http Response. From what I've been researching, my method is completing before the response comes back, which should only take several seconds, but the future is complete before it responds. I've seen there are continuation object options as well, but since futures are Static methods, I'm not sure that is an option. Here is what I have so far:
 
Public class Provision_Trial_Trigger_API_Callout {
   
   @future(callout=true)
   public static Void execute(Map<String, String> config) {
       try {

            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint(config.get('endpoint'));
            request.setMethod(config.get('method'));
            request.setBody(config.get('body'));
            config.remove('endpoint');
            config.remove('method');
            config.remove('body');
            for (String key : config.keySet()) {
                request.setHeader(key, config.get(key));
            }
            HttpResponse response = http.send(request);
            //the above line appears to be causing the function to stop
            //and no line below it or above it will execute if not commented out
            if (response.getStatusCode() == 200){
                 //do stuff
            }else{
                System.debug('fail');
            }
        } catch(DmlException e) {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
    }

}

I've tried to make a wait() function, but this function doesn't work when the 'HttpResponse response = http.send(request);' line is not commented out.

Any thoughts or suggestions how to overcome this? I'm open to using Continuations, but unsure of when they are to be used.
Raj VakatiRaj Vakati
Once your feature is completed and you got the reposne .. update the response in custome fields or create a custom flag to makr the status of feature is completed .. 

from there you can use .. 
 
Public class Provision_Trial_Trigger_API_Callout {
   
   @future(callout=true)
   public static Void execute(Map<String, String> config) {
       try {

            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint(config.get('endpoint'));
            request.setMethod(config.get('method'));
            request.setBody(config.get('body'));
            config.remove('endpoint');
            config.remove('method');
            config.remove('body');
            for (String key : config.keySet()) {
                request.setHeader(key, config.get(key));
            }
            HttpResponse response = http.send(request);
            //the above line appears to be causing the function to stop
            //and no line below it or above it will execute if not commented out
            if (response.getStatusCode() == 200){
                 //do stuff
// YOUR object .Responce__c = response.getBody() ; 
// YOUR object .Responce__completed__c=true ; 
​


            }else{
                System.debug('fail');
            }
        } catch(DmlException e) {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
    }

}

 
Turner NelsonTurner Nelson
It doesn't appear to change the outcome.

If I put this following line inside the if state to print the response body, then I check the Developer Console, it never reaches that line, or the 'fail' comment. It appears the HttpResponse response = http.send(request); line is halting the execution.
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200){
     System.debug(response.getBody());
}else{
     System.debug('fail');
}

 
Raj VakatiRaj Vakati
Looks like your intgeation is failing 

Refer this code
 
Public class Provision_Trial_Trigger_API_Callout {
   
   @future(callout=true)
   public static Void execute(Map<String, String> config) {
       try {

            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint(config.get('endpoint'));
            request.setMethod(config.get('method'));
            request.setBody(config.get('body'));
            config.remove('endpoint');
            config.remove('method');
            config.remove('body');
            for (String key : config.keySet()) {
                request.setHeader(key, config.get(key));
            }
            HttpResponse response = http.send(request);
            //the above line appears to be causing the function to stop
            //and no line below it or above it will execute if not commented out
            if (response.getStatusCode() == 200){
                 //do stuff
<b>// YOUR object .Responce__c = response.getBody() ; 
// YOUR object .Responce__completed__c=true ; 
​</b>


            }else{
				
				<b>// YOUR object .Responce__c = response.getBody() ; 
// YOUR object .Responce__completed__c=false ; 
                System.debug('fail');
            }
        } catch(DmlException e) {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
    }

}

 
Raj VakatiRaj Vakati
Is its wokring ?
John PiescoJohn Piesco
// FIRST: Set a longer request timeout when setting up the HttpRequest object
// as default may not be long enough,

HttpRequest request = new HttpRequest();
request.setEndpoint(config.get('endpoint'));
request.setMethod(config.get('method'));
request.setBody(config.get('body'));
request.setTimeout(60000); // sets timeout to 60 seconds  <-- ADD THIS LINE 

// THEN: Add a while loop immediately after sending the http request and wrap the request in a try catch block like so,

// declare an HttpResponse object without instantiating it 
// so you can access it outside of the try/catch if necessary
HttpResponse response;
try{
    // instantiate HttpResponse object
    response = http.send(request);
    // add the following while loop after sending request
    while(response.getBody() == null) {
        // Wait for the response to complete by waiting for the reponse body to be set.
        // No need to do anything else in this wait loop.
    }
    system.debug('request completed successfully!');
}
catch{
    // if 60 seconds elapses without response
    // the request will timeout
    // handle request timeout failure here
    system.debug('request failed!');
}

// nice :-)