Update N Records in Flow Without a Loop

Thanks Alex Edelstein and Narender Singh for building exciting Invokable Apex Actions that extend the use of Flow for admins!

USE CASE: The Ugly Dog Adoption Agency wants to find the 30 dogs who have been at the shelter the longest (minimum 60 days). A Flow will change the picklist “Old Timer” to “Yes” and the staff will then run a promotion for these special doggies. (Don’t worry, Jo Jo Pumpkins! You’ll be on the local news!)jo jo pumpkins

dogflowN

Why This is Awesome

  • This is the first time that we can tell Flow to return a specific number of records without doing a ridiculous loop and counting thing.
  • We can update fields on all the records in our collection without doing a loop! It’s one simple action.
  • These Invocable Apex Actions are delivered by a Salesforce staffer and a community member. They aren’t fully baked solutions, but are great examples of how we can all work together to do awesome shit.

Step by Step

[Side note: That New Kids Song came out 30 years ago!]

  1. Install this unmanaged package from UnofficialSF.com (Alex E)
  2. Create an Apex Class with code from ForcePanda (Narender S)
  3. Create a new Flow.
  4. Drag an Action over from Elements. Find “Get N Records with WHERE Clause Filter.” That’s the class you just made.
  5. Here’s how I filled this out for my custom object Animal__c and custom fields. I had to use my phone-a-friend to figure out the SOQL where clause for “the dog was found earlier than 60 days ago.” (Thanks Andy Szymas!) I was glad to find the WHERE clause also takes “ORDER BY” which allowed me to order the records by oldest (date found, ascending). So this bit requires a little bit of SOQL skill. To learn, start here and find the SOQL trails on Trailhead.animal Nrecords
  6. I drag the next Action onto the canvas which is MapCollection from UnofficialSF. You can read all about that on this blog post. Here’s how I filled it out in this case. My input collection was created by the previous Action. In KeyValuePairs I’m saying select “Yes” in my field called “Old Timer Picklist” – this is how we indicate this dog has been around for a long time and deserves some extra support. oldTimers
  7. The last bit is very important! Update the records in the collection created in the SECOND action.  (Yeah I spent about 30 minutes trying to update the wrong collection). EditUpdate

THAT’S IT!

//Here's the action "Get N Records with WHERE Clause Filter" written in SOQL.
//This is not relevant to you, but I am learning how to write SOQL.
SELECT Old_Timer_Picklist__c,Date_Found__c FROM Animal__c WHERE Animal_Type__c = 'Dog' 
AND Date_Found__c < Last_N_Days: 60 
ORDER BY Date_Found__c ASC LIMIT 30

 

7 Comments

  1. Very cool. Will this still work without the “Number of records to query” limit? Looking at this as a potential work-around for Flow runtime execution limits (2k).

    Thanks!

    Reply

  2. Is there a performance benefit from using this method as opposed to the Loop method previously available with Flows? Curious as to the comparison of the 2 methods and what would make 1 better than the other for different use cases.

    Reply

Leave a Reply to Update N Records in Flow Without a Loop by Jessie Rymph – UnofficialSF Cancel reply