I have written a trigger that calls a class which contains a future method and I need to use the response to update a field. Is there anyway to capture the HttpResponse in a separate class?

public class web_ser {

public static void web_services_rest(String order_JSON, String order_number){

    String payload_JSON =  order_JSON;
    String endpoint = 'https://some_website.com';

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


    HttpResponse response = http.send(request);

    System.debug('STATUS: ' + response.getStatus());
    System.debug('STATUS_CODE: ' + response.getStatusCode());

    String responseJSON = response.getBody();
    System.debug('responseJSON: ' + responseJSON);


    Map<String, Object> deserialized_JSON_response = 
        (Map<String, Object>)JSON.deserializeUntyped(responseJSON);

    String message = (String)deserialized_JSON_response.get('message');
    String id_number = message.substringBetween('id_number:','}');


    Order order_query = [Select id_number__c
                         FROM Order WHERE Order.OrderNumber = :order_number];

    System.debug(order_query.id_number__c );

    order_query.id_number__c = id_number;
    System.debug(order_query.id_number__c );

    update order_query;


I wrote the below class to send a JSON packet out, receive a response back in and take the value of the response to update the id_number__c field in the Order sObject

  • It's not the class boundary but the transaction boundary that matters here. You can't ping the response back into the trigger handler, if that's what you're trying to do. – David Reed Dec 6 '19 at 19:24
  • @DavidReed would it be best to not use a future method? Is there a way to perform a callout in a class and pass it to another? Would it be best to expose my Apex class as a web service? – Richard Dec 6 '19 at 19:51
  • 2
    @Richard It'd help if you went into more detail about what you're trying to do, and the code you've written so far. Triggers can't make callouts, so some way of passing things off to an async transaction is required. Is there some reason why you can't simply perform a DML operation in your @future method or queueable/scheduled/batch class? – Derek F Dec 6 '19 at 20:01
  • @DerekF added my code – Richard Dec 7 '19 at 2:08
  • 1
    why does the above not work? if called by the trigger, it executes async and updates the order. Only issue would be limits as you can't do 200 future calls if the trigger is handling 200 recs – cropredy Dec 7 '19 at 7:31

Figured out how to get the update to work. Thank you all! Your comments helped point me in the right direction. Skeleton code below

trigger name_of on sObject (after insert, after update){
for(sObject obj : Trigger.New)
    if (!System.isFuture())
        //insert code here
        //call to class containing @future should work 
        //without any of the complaints I ran into
        web_ser.web_services_rest(/*parameters to be passed*/);


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.