Salesforce Lightning Flow — Using Advanced SOQL

It’s surprisingly straightforward to add a complex SOQL query to your Lightning Flow.

The process involves creating a simple Apex class that carries out your query, and then ensuring that class shows up in flow as a declarative building block that can be dragged into your flow.

The key technology that enables you to publish your apex into flows is has the name Invocable Actions. Our (Salesforce’s) documentation on this technology is inconsistent and we’re working on improving it. The most important page is actually somewhat buried. This page is important because it provides a code sample for the best apex design pattern for invocable actions.

Here’s an example of an apex class I wrote to carry out a single, complex SOQL query:

Note that in this pattern, I create a Requests class to store the inputs that my SOQL query will need and a Results class to define the values that my flow action will return to the flow.

In my example, I carry out my SOQL query in two parts. I first retrieve the name of the user and then retrieve a count of all cases associated with that user name.

I then create a Result instance and pack it with the important values returned with my queries:

Finally, I create a List container and add my Result to it. I use a List even though I know I’ll only have a single Result. That’s just part of how the Invocable Actions interface works: each action is expected to return a List (this makes it possible for the interface to support collections and single value responses with a single method).

Because I’ve annotated a method in my AssessCustomerStatus class with @invocableMethod, this functionality will now appear in flow as a declarative building block:

Dragging this flow action onto your canvas allows you to map upstream values to its inputs, and make use of its return values:

If you can create advanced SOQL queries, you‘re enough of salesforce dev to expose them as declarative flow actions. This opens a lot of new applications up to you.

2 replies
  1. Bryan Broome
    Bryan Broome says:

    Is there a way to abstract the specifics out and setting it up to allow the flow designer to enter a SOQL query in the flow builder and have apex perform the function(s) and provide the information back?


Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply