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