How To: Use Flow to Report on the Most Recent Record (Enhanced Setup and Extended Use Case)

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:

Rakesh Flow Overview

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 UpdateUpdate 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.

Rakesh Flow 1

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.

Rakesh Flow 2

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:

Kris Process Overview

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.) 

Kris Process 1

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.)

Kris Process 2

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:

Process 3

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.

Kris Flow Overview

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:

Flow Top Five

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:

Most Recent - Update All Tasks

 

The second Update Records action then applies only to the source record triggering the process, and marks only that one as ‘true’:

Most Recent - Update This Task

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.

24 thoughts on “How To: Use Flow to Report on the Most Recent Record (Enhanced Setup and Extended Use Case)

  • Hi,

    I am trying to this using process builder and I so new to this I am really lost pleas can you help me, I have a related list on the contact and I need to mark the most recently created record as Currently Active by populating a tick box along with ensure any previous records have the box blank.

    I hope you can help.
    Lorr

    • Hi Lorr – thanks for the comment. Let’s see if we can help you.

      Essentially, your process will be defined as follows:

      Criteria: Fire process when a record of the related object/list is created. For ease, let’s say your related object is, I don’t know, an Insurance Policy. So your process should start whenever an Insurance Policy record is created.
      Immediate action #1: You’d start from your new Insurance Policy record, then go up to the related Contact, and then down to all related Insurance Policies. The ‘records to update’ will therefore end up looking something like this: Insurance_Policy.Contact.Insurance_Policies. The action on these records is to set the Currently Active field to FALSE.
      Immediate action #2: This time, you only need the single Insurance Policy record which began your process, so you don’t need to ‘go’ anywhere. The ‘record to update’ will therefore end up looking something like this: Insurance_Policy. The action on this record is to set the Currently Active field to TRUE.

      Do let us know how you get on and whether we can help further. Thanks.

  • The process failed with error message too may DML rows 10001. Any ideas how to avoid this problem? We really need to be able to report on most recent task.

    • Hi RM… Am I right in my assumption that there are indeed more than 10,000 task records which need to be updated as part of this process? So there is a single account (for example) with more than 10,000 tasks?

      If so, this is typically the kind of range where you start running into platform issues like data skew, and this DML limit. Because that is the DML limit, unfortunately, and I’m not sure there’s much you can do in your process to avoid this.

      The only thing I can suggest is to apply some filter to the kind of records the process updates. Is there really a need for it to update more than 10,000 records? Perhaps only update task records created in the last year, for example? Outside of that range, they are unlikely to be marked as ‘most recent’ anyway, right?

      Or, perhaps, there is some issue with your process configuration where the process thinks you’re trying to update all tasks in the org, rather than all tasks against a certain record?

      • Hi Chris

        Thanks for your swift reply. There are no Accounts in our Org that have more than 10000 tasks, the highest is around 700.

        I copied the process above so don’t think it’s trying to update all tasks though perhaps I did something wrong. I tried to filter by record type but this wasn’t possible as sometimes the task is on an Account and sometimes on a Contact.

        I will try your suggestion to filter by time instead, thanks.

  • Do you have any guidance on how (in flow) to get the *nearest* x many object A records to specified object B record? SOQL allows Geo Location Distance formulas, and I can work all this out in SOQL Sort by the distance calculation, and (LIMIT 5) etc, but how in flow to store / use the COMPARISON between an object of 1 type and another? I don’t think a field on object A is the best idea, since I’ll want to loop through MANY Object B’s looking for their nearest A’s.

  • This is the exact problem I’m trying to resolve on a custom object, but it appears that the images to illustrate the flows and process builder are no longer available. Are they available somewhere else or could they be reposted? Thanks for this awesome resource.

    • Hey Luke – please revisit the article now, I think I’ve fixed the problem with the missing images. Thanks for letting us know!

  • Hi Chris,

    I’ve got this version of the process running smoothly in my org, but I actually need to have the flow pulling the most recent based off another date field and updating the account based on that “most recent” record. Is there a simple enough way to do that that you can think of?

    Thanks!

    • Hi Maggie,

      Where is the other date field you need to pull from? Another field on the same object you’re looping through in the flow? Or on some other related object?

      Essentially the trick will be to sort your collection by THAT other field and ensure you’re looping through the collection based on that new ordering.

      If you can let us know more about the objects and their relationships/fields, we can try to help further.

      Thanks,

      Chris

  • Hi,

    I have used this process and flow in the past and it worked perfectly with a little guidance from you, however I have now tried to use it in reverse.

    I am trying to populate a tick box on a junction object “Carer Placement Record” that has two related contacts one a “carer” and the other a “client”, I need to look only at the carer contact and tick the box if the “Scotland Client” tick box is true.

    Is this possible with this process?

    I have tried everything I am just getting no where so any help or advice will be much appreciated.

    Thank You,

    Lorr

  • Hi Chris,

    I want to report on only the most recently created task and also the most recently completed task. Will the above process work for this? I created workflow that checks a box whenever new task are created. But this have to be unchecked when another new record is created.

  • Hi Chris, great post!

    Any idea whether it is at all possible for the ‘Other Task Not Most Recent’ action to traverse up to a parent Opportunity record too, and then back down again to all related child Task records?

    The only option in Process Builder appears to be ‘[Task].Related To ID (Opportunity)’ which doesn’t give you the option to then select the Task IDs and update the ‘Most Recent Task’ field to ‘False’ on the Task object.

    Thanks & regards,
    Regan

  • Hi Chris,

    I followed all the steps. I successfully created a flow. But on Process builder, Select and Define Action > Set Flow Variables and here I am unable to find Case id and account id against CaseVar and AccountVar. Is there any reason because my case and account are not master detail relationship? Its lookup relationship.

    Quick response will be much appreciated.
    Regards

  • I had no problem executing this using Process Builder for tasks. However, what I really want is to find, mark, and report on the Latest activity on an Opportunity regardless of whether it is a Task or Event. So far I can mark the latest task or the latest event but I cannot figure out how to make it work without having two latest activities (one Task and one Event)

  • Hi Chris

    I followed this article and was able to built a Flow and Process for my case object (Mark recent Case tick box when a Case is created). Thanks a ton for that. Very much Appreciated.
    Now again I am trying to create similar thing on my Notes Object (Custom Object). This time I want my process to start when a Note is created or edited. But its giving me error when in Process builder, I select Start this Process when a record is created or edited. As you mentioned above, in Process Builder step 1, it should work. But its giving an error.

    Error element Update_Other_Notes (FlowRecordUpdate).
    The flow tried to update these records: null. This error occurred: DUPLICATE_VALUE: Maximum number of duplicate updates in one batch (12 allowed). Attempt to update Id more than once in this Api call:

    Can you please help me with this?
    Your help will be highly regarded.

    Thanks in advance

  • Hi Chris,

    I’m having issues with the process builder. For my case scenario we use Assessments that are related to a Record.

    The first action works perfectly by itself. Sets the checkbox to false for all related Assessments. Record is Assessment.Record.Assessment.

    When I add the second action, all related Assessments has the checkbox checked instead of the Assessment that triggered the Process. The record for that is just Assessment so I’m not sure why is doing that.

    Do you have any idea on why is happening or any fix?

  • I have a somewhat related process I want to try using some akin to your methodology. I have a 3 attempt process that I need to mark the most recent attempt as either 3, 2 or 1 depending whether 1 or 2 additional prior attempt records are found. So, for example if I push the record into the flow via Process Builder and no other records are found, mark the attempt as 1. If 1 additional record is found, then mark the newest record as 2 and the 2nd older record would still be 1, if 2 additional records are found then mark the newest as 3, the second older as 2 and the most old would be 1. I know it’s a little backwards. Each time I think I have this down in my head as doable, I start thinking it’s not 🙂

    Is this doable you think?

  • just to further clarify this I would need some type of comparison between my “not most recents”. in your example all not most recents are marked the same. Essentially at least 1 record needs to be remarked from 1 to 2 and the newest as 3. But it needs to account for whether 1, 2 or 3 records exists in order to make the attempt marking. I would try to prevent more than 3 records from entering into the flow by marking some other limiting field so this doesn’t get nuts.

  • Hi! thanks so much for this – I think it should solve a long-running problem of mine. Question: Would it not make sense to put an additional filter on the 2nd flow node of “Is Latest Record” does not equal False? That way it avoids even looking at the records that are already not marked Is Latest. Or, is the flow by default intelligent enough to ignore those records?

Leave a Reply

%d bloggers like this: