As part of our series of #FlowFeb blogs, we recently shared this example of how Flow can be used to help report on only the most recent child record. By creating a Most Recent Record checkbox field on the child object and using Flow to maintain this field across all records whenever a new one is created, you gain the ability to run Parent with Child reports without having to manually filter out all the children you don’t need to see.
Soon after publishing the post, two community members got in touch – one with a suggestion as to how the flow could be simplified and optimised, and one with a question about whether this kind of flow would open doors to an alternative use case. We were impressed with the possibilities, so wanted to publish an add-on article dedicated to these.
We will be leaving the first article live, since although what it achieves could be built in a more simple way, we feel it serves as a useful step-by-step guide as to what each flow element can do and how they are configured. Plus the additional use case we will discuss here requires a more complex flow, so we will use the original as the basis from which we explain how to build that out.
OK, let’s get started…
How To Optimise Your ‘Most Recent Record’ Flow
The original flow we built has nine elements. If someone told you that you condense this to just two, you might be doubtful. Unless that someone was Rakesh Gupta, Salesforce MVP and Flow expert. What Rakesh showed me helped me not only to optimise my flow but to understand the differences between the Fast Update and the Record Update flow elements. I’ll explain below how to build the flow in my own words – but all credit goes to Rakesh for conceiving such an elegant solution.
Preparing to Build Your Flow
As before, you should bear in mind that Flow (or Visual Workflow) is available in Enterprise Edition orgs or higher, and that if you’re new to Flow then reading this implementation guide will be a good starting point. When you’re ready to build the flow, navigate to Setup > Create > Workflow & Approvals > Flows, then click New Flow.
Building Your Flow
This part just got a whole lot quicker thanks to Rakesh’s neat Record Update trick. Look how simple our new flow will turn out:
Although there are now only two elements we need to create, we’ll still tackle each of them in turn to show you the full set up of each step.
1) Record Update – Update Latest Case: This step will mark the record entering the flow as being the most recent one. We do this by configuring a Record Update which filters for, in my example, the case which matches CaseVar, a variable I have created within the flow and set to Input Only. At the moment the flow has no way of knowing what the CaseVar is, but we’ll pass that in later using the Process Builder. Once we have told our element to filter only the case being passed in, we perform a field update to change our Is Latest Record checkbox to true.
2) Record Update – Update Other Cases: This step will mark all the other sibling records of our case as not being the most recent one. We do this by configuring a Record Update which filters for the cases’ Account ID matching AccountVar, a variable I have created within the flow and set to Input Only, but only where the cases’ IDs do not match our existing CaseVar variable. Once we have told our element what cases to apply the update to, we specify a field update to change our Is Latest Record checkbox to false.
Completing and Launching Your Flow
With both elements added to the canvas, connect them together and select your first element – Update Latest Case – as the start element. Save, close and activate your flow.
As before, we now need to turn to our new friend, the Lightning Process Builder, to build a simple process which detects a new case being created and passes that case’s ID and AccountID into our flow, causing it to execute the record updates.
To begin creating your process, navigate to Setup > Create > Workflow & Approvals > Processes (or Process Builder), and click New Process.This is what the finished process is going to look like – and it won’t take us long to get there so let’s walk through the steps:
1) Click on Add Object. In the right hand pane, type in Case, select it, leave the other option as ‘created’ and click Save. (If your use case was slightly different here you may want to choose ‘created and edited’ instead – for example if you wanted to store the most recently closed opportunity.)
2) In the diamond-shaped Add Criteria node, select No Criteria and save. (If your use case was different you may want to set up some specific criteria.)
3) Under Immediate Actions, click Add An Action, select Launch A Flow, select the flow you just created and then pass in the Case ID and the cases’s Account ID into your flow variables like this:
Now simply save and activate the process, and you are done! You now have a new field on your child object that can be used in report filters, and a process and flow to keep the field up to date. And this time you got it done in two elements rather than nine – awesome!
Extend Your Original Flow with a New Use Case
The simplified flow above is great if you always want the one record entering the flow to be the ‘true’ record and everything else to be ‘false’. But community superstar Geoff Flynn soon posed an interesting question – could our original flow be adapted to tag the last five, for example, or the top five based on a metric? It seemed possible and two leading lights of Flow knowledge – Rakesh (again!) and John Stevens – soon confirmed it would work. And here’s how…
For this more complex use case we will go back to our original nine-step flow. Below is a summary of how that looked, and here is the full guide to putting it together.
So what do we need to add to or remove from the above to get it to flag the latest five or top five records?
- Consider removing the first Assignment element, since the case entering the flow may not be in the five you want to make true, especially if you’re returning a top five rather than a latest five.
- If you do remove the first Assignment, then ensure your LookupOtherCases element is adjusted to return all cases, without the filter for ID not equal to Case ID.
- In your LookupOtherCases, check the Sort Results By option and select a field and order to sort the collection by. For example, you might choose to order cases by Created Date descending, or you might sort opportunities by Amount descending.
- Inside your loop, increment a new counter (a number variable with scale 0 and default value 0) by 1 for each iteration. Follow this iteration with a decision element to check for the counter’s size. If the counter is still at 5 or under, route to an Assignment which sets the record’s checkbox field to ‘true’. If the counter has exceeded 5, route to a separate Assignment element which changes the checkbox to ‘false’. Both of these Assignments can connect back to your existing Assignment element to add the records to a new collection.
- If you removed the first Assignment, then you can also remove the Update Latest Case element since your latest record will now be included in the main collection.
Your extended flow should end up looking something like the below:
We hope that the simplified flow helps those of you getting started with Visual Workflow, and also that the extended use case shown here gives you an idea of how powerful Flow can be. Huge thanks to those on the community who gave the suggestions that went into this follow-up blog, especially Rakesh for his super-smart simplification. The community rocks!
Footnote (March 2016):
Soon after posting this blog last year, I realised that for the simpler use case, you needn’t even use Flow, since it can be satisfied using Process Builder alone. Thank you to Jennifer Lee for finally giving me the push I needed to write this part in. It’s long overdue!
This is done by having two Update Record elements as the actions in your process. The first unchecks your Most Recent checkbox field from all records in the collection (by traversing up to the source record’s parent and then back down again to that parent’s child records), as below:
The second Update Records action then applies only to the source record triggering the process, and marks only that one as ‘true’:
Since the actions in a process are fired in the order they are defined, this results in all records in the set being marked as false first and then marking the single record firing the process as true.
So if you’re looking to achieve this in the quickest, simplest way possible, Process Builder is the way to go. But hopefully the main article above still gives you an interesting way to achieve this while helping you learn Flow at the same time.