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

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());
        }
    }
}

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());
        }
    }
}
added screenshots from the flow
Source Link
Bahman.A
  • 928
  • 4
  • 17
  • 40

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 aan 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 triedset to be clearvariables for the response status code and response message with my question but please let me know if I needvalues that I'd like to put more information herebe returned as the actual response status code and response message. I've made both of these variables available for output. enter image description here

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 a 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. I tried to be clear with my question but please let me know if I need to put more information here.

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

Source Link
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 a 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. I tried to be clear with my question but please let me know if I need to put more information here.