One Rest API for your Lifetime Salesforce Implementation with Flow Builder as Controller

Munawirrahman
5 min readDec 20, 2020

Hi Folks, in this article, I’d like to show you how to reuse one Rest Apex Class (+test class) for your lifetime Salesforce implementation.

For those who still not see my article about JSON Parsing in Salesforce Flow, I would suggest you to visit here and here to see how I managed to parse JSON inside Flow Builder because in this article, I will use those components to process inbound data from the rest API.

You may already know that, we can launch Flow directly from Rest API from this official Salesforce Documentation. So the question is, why would I use this method if Salesforce already has default way to launch Flow from Rest API? My personal answers are:
1. That method only support 1 HTTP method, that is POST. And sometimes, we need to use other method when we’re dealing with Salesforce Rest API
2. If you use Official Salesforce method, you’ll not able to custom your own JSON Structure, you’ll have to follow their pattern. And sometimes, that pattern won’t work for your integrated system (or I’d say, its a very awkward structure)

Okay okay, so what solutions am I offering here?
My solutions here are to:
1. Have one Rest Apex Class (+test class) that basically would able to control all of your rest requests (And every time you want to expose new function, you just need to update the flow and don’t need to add new test class)
2. Route the flow functions based on path you set in your flow builder
3. As you may already know, Flow has functionality to send error message to admin when there’s something wrong with your flow. So you’ll immediately notified if there’s something wrong with your integration
4. Supports POST,GET,DELETE,PUT,PATCH methods
5. Able to catch request headers in your flow
6. Able to have dynamic JSON structure

So, here are the steps you need to do to setup that solutions:

a. Install this package which contains OneRest Class, OneRest Test Class, And Main controller for your Rest API. : https://login.salesforce.com/packaging/installPackage.apexp?p0=04t2w000009F5cg (Don’t forget to change login to test.login if you want to install in Sandbox)
Source Code: https://github.com/munawirrahman/OneRest

b. After you installed that Package, congratulations, you already have your first rest with ping pong functionality

Image1: OneRest is the default path name for your installed package, feel free to change it in the apex class (and test class) if you need to

c. After you already installed that package, you’ll have an AutoLaunched Flow like this

Image2: Initial installed flow
Image3: Decision element, as your router based on path
Image4: How to return pong
Image5: Return error code 400 if no path found
Image6: Return 400 if no path found

d. Here are the variables that you can use to route and process the request

Image7: Variables to process requests

Usage Examples:
1. API Specification: GET API to Count Accounts based on Country

Image8: Add new path to Router Decision
Image9: Parse Params JSON field country with MultipleJSONParser Action (value stored to QueryStringCountry)
Image10: Add Get Records Element and use parsed data as filter (country)
Image 11: Count total records found with Assignment Element
Image 12: Assign body,content-type, and status code for return
Image 13: Result
Image 14: Final flow for Spec GET CountAccounts based on country

2. API Specification: Patch Lead Status API with lead id in 2nd path

Image 15: Add new decision in Router
Image 16: Use Multiple JSON Parser to parse Content-Type from headers and store to content_type variable
Image 17: Check is request JSON or not, if not return 400 status code
Image 18: Parse field NewStatus from RequestBody / payload
Image 19: check if there’s status field in payload / body or not (if not, return 400)
Image 20: Update Lead based on path2 to status from RequestBody
Image 21: Return result in JSON
Image 22: Result in workbench
Image 23: Final Flow

There you go, Now you won’t need to add new apex class every time you want to open new custom API to your org and of course, you can use Flow Builder as your Controller for automation.

Things to consider:
1. Only supports max 5 paths (my personal judgement says this should be enough)
2. Use Subflow everytime you create new route, It will make the main flow/controller not looks like ramen(eventho I like ramen)
3. Feel free to adjust the main urlMapping or flow controller based on your need. But don’t forget to adjust the test class if you change it

--

--