0

I call an invokable apex class method, that in turn calls an external REST service. How do I send the http response back to the flow? Am I missing the invokable return variable?

    public class obuTopUpTrailhead {
// Call this method from flow
    @InvocableMethod
    public static void invokeapexcallout(list<String> obuDetails){
    for (string s : obuDetails){
    PutCallout(s);
    }
}   

// http callout method
public static HttpResponse PutCallout(String pName) {
    System.debug('Incoming String is :' + pname);
    String FullString   = pname;
    String[] OBUdetails = fullstring.split(',');
    String recordid     = OBUdetails [0]; 
    String obusn        = OBUdetails [1];
    String topup        = OBUdetails [2];
    String cardnumber   = OBUdetails [3];
    // Make JSON conversion
    JSONGenerator gen = JSON.createGenerator(true);    
    gen.writeStartObject();      
    gen.writeStringField('OBU Record ID ', recordid);
    gen.writeStringField('OBU S/N', obusn);
    gen.writeStringField('Top up Amount', topup);
    gen.writeStringField('Card Number', cardnumber);
    gen.writeEndObject();    
    String jsonS = gen.getAsString();
    System.debug('jsonMaterials'+jsonS);
   
    // Sending the http body with JSON
    String endpoint = 'http://www.example.com/service';
    HttpRequest req = new HttpRequest();
    req.setEndpoint(endpoint);
    req.setMethod('POST');
    req.setbody(jsonS);
    Http http = new Http();
    HTTPResponse response = http.send(req);
    
    // Parse the JSON response
    if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
            response.getStatusCode() + ' ' + response.getStatus());
    } else {
        System.debug(response.getBody());
    }
    return response;
}

}

0

Funny... I just got done figuring this out myself:

You have to first create a class for all the values in your json response body. Then, deserialize the response.getbody() referencing the newly created class.

Does that make sense?

| improve this answer | |

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.