1

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());
        }
    }
}
3
  • at least for apex actions invoked by the API, if the apex action throws an exception, the REST client gets back a 400 and an array of status details. You may need to invoke an apex action that throws an exception to make this work
    – cropredy
    Commented Dec 7, 2022 at 17:04
  • @cropredy thanks for your comment, so just to make sure i understand correctly. you're saying i need to create an apex action that then in case of required values missing i just call that apex action from within my flow and then have the apex action throw an exception? how do I set the response message then? or if I want a 422 status code to be sent instead of a 400?
    – Bahman.A
    Commented Dec 7, 2022 at 17:12
  • so - I have limited experience here and only with apex actions. If they threw an uncaught exception, it was a 400. You might need to change your REST endpoint to be an ApexRest call that then invokes your flow, inspects the flow output variables and sets status code to 422. But others may have more experience on this than me
    – cropredy
    Commented Dec 7, 2022 at 17:40

0

You must log in to answer this question.

Browse other questions tagged .