Skip to main content
3 of 3
added an apex example
Bahman.A
  • 928
  • 4
  • 17
  • 40

How to set the statusCode and message of a rest api call from an autolaunched flow?

I have an autolaunched flow that I'm invoking with a rest api call. Here's the sample code that shows how I call my flow from an express.js server.

var _request = {
  url: '/services/data/v54.0/actions/custom/flow/FlowName',
  method: 'post',
  body: JSON.stringify(
    {
      inputs: [
        param1: value1,
        param2: value2,
      ]
    }
  ),
  headers : {
          "Content-Type" : "application/json"
      }
};


const salesforce = config.getProperty('SalesforceRest');
const conn = new sf.Connection({ loginUrl: salesforce.host });

try {
  await conn.login(salesforce.userName, salesforce.Password + salesforce.Token);
} catch (error) {
  return res.status(500).send(errorResponse.ServerError(`Salesforce login failure: ${error}`));
}

conn.request(_request, function(err, resp) {
  console.log(resp);
});

I'd like to know how to set the statusCode and the response message values in my flow. For example to return a statusCode = 400 and message = 'Missing required value' when my flow runs and finds out there's a missing required value. My flow looks like this: enter image description here After checking for required values, if there's one or more missing, I set to variables for the response status code and response message with values that I'd like to be returned as the actual response status code and response message. I've made both of these variables available for output. enter image description here

NOTE: I'm trying to stay away from apex and see if I can do this all together with the flow. using apex I could create the rest endpoint in apex and invoke the flow from there and then return the response as I'd like to in apex like the following, and that's how I'm doing it right now, I want to see if I can avoid the apex code.

@RestResource(urlMapping='/my_flow')
global class MyApexAPI {
    @HttpPost 
    global static void doPost() {
        RestResponse res = RestContext.response;

        try {
            Map<String, Object> req = (Map<String, Object>) JSON.deserializeUntyped(RestContext.request.requestBody.ToString());
            Flow.Interview.MyFlow myFlow = new Flow.Interview.MyFlow(req);     
            myFlow.start();
            res.responseBody = Blob.valueOf(String.valueOf(myFlow.getVariableValue('response_message')));
            res.statusCode = Integer.valueOf(myFlow.getVariableValue('response_status'));
        } catch (Exception e) {
            res.statusCode = 500;
            res.responseBody = Blob.valueOf(e.getMessage());
        }
    }
}
Bahman.A
  • 928
  • 4
  • 17
  • 40