One Rest API for your Lifetime Salesforce Implementation with Flow Builder as Controller
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
c. After you already installed that package, you’ll have an AutoLaunched Flow like this
d. Here are the variables that you can use to route and process the request
Usage Examples:
1. API Specification: GET API to Count Accounts based on Country
2. API Specification: Patch Lead Status API with lead id in 2nd path
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