How to set the Flow Finish Behavior to a newly created Record WITHOUT an additional Screen

One of the biggest issues that many Flow Users have when they’re looking to build a Visual Flow, is that they can’t redirect the User to the newly created Record without having an intermediary Flow Screen confirmation page.  It adds one pointless click into the mix that we as Admins always try to reduce throughout our Orgs.   What if you didn’t have to use How to pass a new variable out of Flow post to do this?  There might just be another way!  Let’s breakdown the concept of the End Users’ process at a high level…

FlowVFRedirect.jpg

While we are still going to have to use Apex and Visualforce to make this happen, we’re now able to do it without the extra Screen.  This is because, by the time we Redirect our End User to the Visualforce Page, the newly created Record is in the system.  We can then use a really simple SOQL query to find the newly created Record and immediately send them to that record.

Let’s break this down further and talk about the first piece of this process… our Visual Flow and how it can be launched.

The Visual Flow can be launched with either a Visualforce Page or through the URL.  I would typically aim for the URL, if you’re in Classic, as you can take advantage of the new Flow Skin.  For this blog post, that’s the method we’ll be using, and we’ll be doing it through a Button.

In this scenario, to keep it simple, we’re going to have the Flow create a Contact and then redirect us to that Contact record.  So, we need to create a Text Field called Unique Flow Identifier on the Contact for us to pass in as a variable to the newly created Record.

Unique_Flow_Identifier.jpg

We’ll cover what goes into this Field shortly, but let’s take a look at our Flow first.

Create Contact Flow.jpg

We’ll need to create our AccountId and UniqueId Variables, and ensure they are listed as Input variables.

AccountId

Unique_Id

Notice how we are passing in our Unique Identifier into the Contact on creation.  This is how we will query to find it.

Record Create

Our Flow is all set, so we can Save and Activate it.

Now, we’ll get to the Visualforce Page and Apex in a moment, but first let’s assume that we’ve already created our Visualforce Page called FlowRedirect so we can talk about our Button’s URL.

/flow/Create_Contact?AccountId={!Account.Id}&UniqueId={!$System.OriginDateTime}{!Account.Id}
&retURL=/apex/FlowRedirect?Id={!$System.OriginDateTime}{!Account.Id}

In this, I am using the Account ID and System Origin Date Time to combine together and be my Unique Identifier.  You could take this a step further and also use the User ID or another parameter.  If you plan to re-use the same Visualforce Page for multiple Objects, you’ll want to pass in another parameter to let you know what Object you want to query.

Alright… let’s create our Apex Controller.

FlowRedirectController.jpg

public with sharing class FlowRedirectController {public with sharing class FlowRedirectController {
public Object FlowRedirectController() { String unique_id = ApexPages.currentPage().getParameters().get(‘id’);
if(unique_id == null){
// Return Home if no ID String url = ‘/home/home.jsp’; return new PageReference(url); } // Get Contact ID and set Redirect String contactId = [SELECT Name,  Unique_Flow_Identifier__c, Id  FROM Contact  WHERE Unique_Flow_Identifier__c = :unique_id ORDER BY CreatedDate DESC LIMIT 1].Id;
// Did we find a Contact? if (contactId == null) {
// Return Home if no ID String url = ‘/home/home.jsp’; return new PageReference(url); }
// Redirect to Contact String url = ‘/’ + contactId; return new PageReference(url); }}

Our Apex Controller is grabbing the id that we send in through our Button, and then doing a quick Query on the Contacts in our system to find the match.

Now for our Visualforce Page, which is going to be bear bones, because the End User shouldn’t hopefully ever seen it.

Visualforce Page.jpg

<apex:page controller=”FlowRedirectController” action=”{!FlowRedirectController}”>
</apex:page>

Our Visualforce Page is doing an Action of calling our FlowRedirectController on the opening of the page.  Resulting in an immediate redirect to the newly created record.

Let’s watch it in action!

2017-07-23_16-51-42-1

And just like that we have bypassed the “must have a screen” feature in Flow for a redirect.  I’ll be looking to get something on GitHub in the near future to clean this process up further, and provide some test coverage.

 

2 thoughts on “How to set the Flow Finish Behavior to a newly created Record WITHOUT an additional Screen

  1. Tamar Erlich July 24, 2017 / 2:19 pm

    Great post about a problem I’ve been struggling with forever. Can this be used with an auto-launched flow with no screens at all?

    Like

    • David Litton July 25, 2017 / 7:15 pm

      Yes, it could! No Screen required for this to be used.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s