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!)
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!]
- Install this unmanaged package from UnofficialSF.com (Alex E)
- Create an Apex Class with code from ForcePanda (Narender S)
- Create a new Flow.
- Drag an Action over from Elements. Find “Get N Records with WHERE Clause Filter.” That’s the class you just made.
- 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.
- 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.
- 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).
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
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!
Yep! Check out this post that didn’t have that limit.
Fantastic…trying it now!
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.
hi hi! I’m tweeting to the expert Narender Singh to see what he says 🙂
In conclusion, Flow combined with Apex is going to run a LOT faster and be able to process more records.
Here’s the whole conversation to read from Twitter. https://twitter.com/ForceTheSales/status/1238516416275349504