Since Winter ’17 release introduced invocable processes and now we can design solutions with reusable processes, it got me thinking about how much reuse we could get out of Process Builder and Flows, two of the most powerful declarative automation features in the platform!
So at the time of the release, these were the ways in which Processes and Flows could be invoked by each other:
- Processes invoke Processes
- Processes invoke Flows
- Flows invoke Flows (sub-flows)
- Flows invoke Processes???
Well, to my knowledge, Salesforce does not have a way to let Flows call invocable Processes, the Flow would have to create or update a record to cause Process Builder to fire in the traditional manner.
This solution is my workaround to enable Flows to call invocable Processes!
How it works is that this solution fills the functionality gap to enable Flows to invoke Processes by way of Invocable Apex that makes an http callout to the Actions REST API.
The benefit is that Flows can on-demand call reusable Processes! Not convinced how awesome this is? Check out Jennifer Lee’s Dreamforce 2016 session on componentized Processes and Flows. The magic is in DRY principles (don’t repeat yourself). Build once, reuse everywhere!
Getting Started
Please note, since the Salesforce REST API requires OAuth authentication, we have to use Named Credentials when making the http request, so there is some extra setup by the admin and potentially by the end-users before this solution can be used.
Please see my github repo for full setup instructions.
1. Create an Invocable Process
2. Create a Flow that uses the ProcessInvocable Apex Action from the canvas palette
3. Configure the ProcessInvocable Apex Action to call your Process.
- API Version – The REST API version to use when calling Actions API to invoke the Process or Flow. Should be 38.0 or greater.
- Named Credential – The Named Credential to use to authenticate to Salesforce REST API when making http callouts to invoke the Process or Flow. You create this once during Setup.
- Process Name – Unique API Name of the Process to invoke.
- Record IDs – Variable Collection of Salesforce record IDs that the Process will execute on. You can alternatively specify Record ID input parameter if you’re only working with a single record.
Thanks so much for creating this, Doug! The ability to invoke process builder from flow is exactly what I need, as I have a scheduled apex class that invokes a flow, but was running into soql query limitations with flow (trying to update all contacts from accounts that meet criteria). So I am hoping process builder will be able to handle the updates more efficiently, by invoking PB from Flow. However, I am running into an error message when I try to execute. This is what I am getting in the debug log:
|FATAL_ERROR|HttpRequestsBatchable.BatchableException: System.HttpResponse[Status=Unauthorized, StatusCode=401]
Any idea what might be causing that? I have downloaded the package and set up the connected app, auth provider, and named credential as per your instructions.
Thanks,
Jennifer
LikeLiked by 1 person
Hi Jennifer,
Thanks for the kind feedback!
Regarding your unauthorized login issue, are you still having issues?
Are you testing this in a sandbox or a production / developer org? I ask because the URLs used when setting up the Named Credential may differ per environment. https://test.salesforce.com would be necessary in sandbox and https://login.salesforce.com for production/developer orgs.
When you get the error, is that when testing as the administrator or as a non-admin user?
Could you provide screen shots of your configurations of Connected App, Auth Provider, Named Credential, and ProcessInvocable element in your Flow?
Thanks,
Doug
LikeLike
Yes, I’m still getting the same error message. I am testing in sandbox as the sys admin. We use a custom domain, so the url is a little different – https://accessintel–full.cs30.my.salesforce.com.
How do I provide the screen shots? They don’t seem to paste here.
Thanks for your help!
LikeLiked by 1 person
Ya, native image sharing isn’t supported in the comments unfortunately. You can share images a few other ways:
1. Share a document with the images in it to Success Community and paste link here, or
2. Use a free service like imgur.com or screencast.com and share link here, or
3. Upload document to a Google Drive or OneDrive or Dropbox or Box folder and share it publically and paste link here
Thanks!
LikeLike
Here’s the screen shots in screencast.com, thanks.
Invocable Process Screen Shot
LikeLiked by 1 person
Thanks, Jennifer!
Perhaps try adding oauth scope “Perform requests on your behalf at any time” to your connected app in addition to full access.
Also try on your Named Credential checking the “Generate Authorization Header” option.
Hope that helps!
Doug
LikeLike
That did it, thanks!
LikeLiked by 1 person
Hi, why does my ProcessInvocable not showing up even though I created the process builder invocable?
LikeLiked by 1 person
Hi Jack,
Without sharing screen shots of your config and your code that is almost impossible question to answer 🙂
Thanks,
Doug
LikeLike
Hi Doug,
I have the same problem with Jack. I created an invokable process builder. I wish to call inside the flow but I can’t find the invocable process in the options/actions.
Thanks,
Ryan
LikeLiked by 1 person
Hi Ryan,
If you are referring to the ProcessInvocable element in Flow, simply type the unique name of your Process Builder, Named Credential, etc. Flow does not provide a list of options here to choose from like you may be accustomed to when entering other inputs.
Looking at the last screen shots in my blog post, I typed all those inputs manually.
Hope that helps!
Doug
LikeLike
Jack,
If you are referring to the ProcessInvocable element in Flow, simply type the unique name of your Process Builder, Named Credential, etc. Flow does not provide a list of options here to choose from like you may be accustomed to when entering other inputs.
Looking at the last screen shots in my blog post, I typed all those inputs manually.
Hope that helps!
Doug
LikeLike
I assume the answer is no, but is there any way to pass variables from the flow to the process builder process other than a record id?
LikeLiked by 1 person
Hi Kevin,
Great question!
When using the Actions API to invoke Process Builder only two input types are supported:
Invocable processes always require either one of the following input parameters:
sObjectId
: The Id of the sObject record that you want the process to execute on. The record must be of the same object type as the one on which the process is defined.sObject
: The sObject itself that you want the process to execute on. The sObject must be of the same object type as the one on which the process is defined.In the current version of my solution, I only support passing the first option,
sObjectId
. But technically you could build up JSON that represents the record and field values you want the Process to operate on.If you use the Actions API to invoke an auto-launched Flow, that’s different. Then your input parameter options are based on which variables are defined in the Flow and exposed as
input
variables.Doug
LikeLike
Hi Doug, Thanks for this great post. I think that this could also be used to launch an invocable process through apex (i.e. apex, calls the flow, which then calls the invocable process builder process). Have you done that?
LikeLiked by 1 person
Hi Starley,
Glad you liked the post and for the kind feedback!
Yes, from Apex you could make the http callout using a Named Credential to Actions REST API to call Flow or Invocable Process.
=)
LikeLike
Overall this technique is working very well… I really appreciate you sharing your efforts.
Occasionally though, (maybe 8 times in the past two weeks) i get the error below… Any thoughts?
Apex script unhandled exception by user/organization:
Failed to process batch for class ‘HttpRequestsBatchable’ for job id
caused by: HttpRequestsBatchable.BatchableException: System.HttpResponse[Status=Bad Request, StatusCode=400]
LikeLiked by 1 person
Hi Scott,
Glad you like it!
I would need more details about the specific error from the debug logs to better troubleshoot. Please open an issue with as much details about the error.
Thanks,
Doug
LikeLike
I wish I had more details. The error never occurs when I interact with the process. It has only happened with other users and I can’t re-create it. It has been followed once or twice by errors in the flow that indicated the flow was trying to update a locked record but 5-6 of the errors were not followed with any other messages. Any idea where to start looking for a reason the batch would fail?
LikeLiked by 1 person
Setup Debug Logs to monitor any users you think are encountering the issue on regular basis maybe?
If your logic is being invoked due to batch jobs or updating multiple records perhaps try reducing batch size?
Without knowing your configuration and what any/all processes, triggers, flows, etc are doing I can’t really point a finger at any one thing, sorry.
LikeLike
Hi Doug, I’m trying to implement a feature where Open leads older than seven days are automatically sent a follow-up email. Your solution here would work for me as I can schedule a flow to call a process every morning.
For the life of me I cannot figure out how to get the variables right on the “Invoke Process” Apex action page. When I debug the flow, it keeps telling me that I need to specify either “Record IDs” or “Record ID”, and I can’t figure out what this means.
I’m using a get records element to gather Leads that are open, then I’m trying to pass them to the process. Do you know what I’m doing wrong? Happy to give more details.
LikeLike
Thannks for sharing this
LikeLiked by 1 person