Salesforce Lightning Flow — Using Advanced SOQL

December 2019 UPDATE: The first actions that take advantage of Spring ’20 enhancements to enables SOQL queries in Flow without needing to create any Apex are starting to get published. Learn more.

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.

Invocable Actions is the key technology that enables you to publish your apex into flows .  Learn more about 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:

Results curResult = new Results();
curResult.severeCaseCount = severeCaseCount;
curResult.revenueTier = curUser.RevenueTier__c;

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:

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.

5 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?

    Reply
  2. Ole Erik Brennhagen
    Ole Erik Brennhagen says:

    Hi! This is really, really neat!

    I do have a question, however, which may also help other novices.

    Your current example handles one returning one by one value, but what if we’d want to return a list over the cases too? How would that look?

    Reply
    • Alex Edelstein
      Alex Edelstein says:

      You’d do that by defining a return variable of type Case[]:

      @invocableVariable
      Case[ ] myCaseList

      Also check out the new link at the top of this post.

      Reply

Trackbacks & Pingbacks

  1. […] into the Apex code and specify the exact object you wanted to use. For example, when I posted this tutorial on injecting advanced soql into flows, you can see that my code specifically declared User and Contact […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply