• John Piesco
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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.