Site icon UnofficialSF

Using Flow to Process Approval Requests Automatically

There’s a bug in Approval Process involving a feature that’s designed to auto-approve an approval request if a specified step entry criteria is not met. This post describes a way to work around that bug.

This feature is normally activated here:

When this feature works properly, you can see it displayed here:

In certain situations, as described here, you can follow the steps but you won’t see the ‘else Approve’. One combination is when the previous step has an Auto-Skip pathway, as shown below. The first step has ‘else Next Step’ and the second step fails to show the ‘else Approve’:

Workaround

The workaround for this bug involves creating an special User account that automatically approves every approval requests it receives, and adding a step that calls that ‘autoapprover’ if the conditions warrant.

Here’s the general flow:

Preparing Your Approval Process For AutoApproval

Let’s go back to our example from above. We have a step that we want to enter if the industry is not Agricultural. If it is Agricultural we want an automatic approval: However, the bug in approval processes prevents the auto approval:

For our workaround, we need to add a step that causes immediate approval if the step gets executed. We’ll do this by adding a new step 2 called Autoapprove Agricultural Accounts that executes immediately before our malfunctioning step. Note that 1) its criteria is set up to be the exact opposite of the original step 2, and it uses ‘go to next step’:

Importantly, We also modify our original step 2 so that it also uses ‘go to next step’. If we leave it on ‘approve record’ it will actually reject the record, which may not be what we want. Basically, since ‘Approve Record’ is broken, you don’t want it in any part of your process.

Assigning the Step to a Special AutoApproval User

We assign our new Autoapprove Agricultural Account step to the newly created User called Autoapprover User:

This is just a normal User, but it has a special email address: 

We’ll touch on that in a moment. First, let’s review what we’ve done so far. After Step 1, our new Step 2 gets evaluated. If the industry is Agricultural, Step 2 will execute as normal and send an email notification to the AutoApprover User’s email account. Otherwise, the approval process will carry on to the original Step 2.

Adding an AutoApproval Capability to your Org

If our new Step 2 gets entered, we want it to cause the request to immediately be approved. To do this, our AutoApprover User uses as its email address a special Salesforce Apex Email Service Address that you’ll generate by setting up an Apex Email Service. This service provides an email address and will auto-process email received at that address. 

Normally, you give each Email Service the name of an Apex Class that you’ve coded up to handle the incoming email. But we don’t want users to need to write code, so we’re going to use a Flow extension that provides Email Services with an Apex Class that knows how to take the incoming email and pass it to a flow. 

Below, you’ll see instructions to create a flow called Auto Approve Request that is designed to be passed an email body by an Email Service. The flow then uses the Find Text action to extract the id of the Approval Process and then uses a new Resolve Approval Process action to approve the request. Here’s what Resolve Approval Process looks like: (UPDATE: this action now also has the input fields ‘objectName’ and ‘recordId’, and you can pass those values in instead of an approvalRequestId, if you wish. This improvement is courtesy of Adityaonmove)

When this action executes, the request will be approved.

Installing the AutoApprover Extensions

Step 1: First, install the EmailToFlow Extension.

This enables any Salesforce inbound Email Service you create to pass incoming emails to a flow.

Step 2: Install Find Text Extension

This is needed to extract the recordId from the email notification that approval processes send out. Here, you can see the id that we need to extract:

Step 3: Generate the ‘Resolve Approval Process’ Invocable Action

Follow the instructions here to generate a Resolve Approval Process action that can be used when the AutoApprover User is assigned a step.

Step 4: Create an autolaunched flow that will autoapprove an approval request

This flow uses the Find Text and Resolve Approval Process actions from the steps above:

To create this flow, do the following:

4.1 – create the following input variable to receive the body of the email notifications:

4.2 – configure the Find Text action to extract the 15 character ID of the approval process from the email notification

4.3 Configure the Resolve Approval Process to carry out an automatic approval

Note the the ‘foundString’ output from the Find Text action is mapped into the ‘approvalRequestId’ input here.

4.4 Don’t forget to activate the flow. 

You might want to test it out at this point by passing it an email body and verifying that it approves the specified approval request. 

Step 5 Update the the Email2Flow apex class to point to your newly created autoapproval flow

When you installed  EmailToFlow in Step 1 above, the package included an apex class called EmailToFlow that gets called by the Email Service and invokes a specified flow. As installed, that class attempts to call a test flow called ‘EmailController’. You need to hand-edit the Apex class in two places to point it at the flow you created above in Step 4. Here’s an example of a class that has been modified to point to a flow called Auto_Approve_Request:

At this point, you can carry out final testing.

Appendix – Troubleshooting

It can be a little tricky to assign an Email Service generated email address to a Salesforce User because of the various confirmation emails that can get sent out. You often need to look at error messages or debug text to find important links or codes. In the following image, when an existing user had their email address changed to be an Email Service address, a confirmation message was sent which generated an error email. Clicking the debug link made it possible to find the necessary confirmation link:

If you have too much difficulty getting this to work, an alternative is to give the auto approver User a free public email address or a corporate email address and then have that account auto forward emails to the Email Service account.

Skip to toolbar