9

Within Visual Workflow ("flow"), I need to evaluate whether an sObject Collection Variable contains any record Ids. I.e., is the sObject Collection Variable null or 'empty'?

If the sObject Collection Variable is null or empty, I simply route the user to a nice screen with additional instructions rather than letting them proceed through the flow to receive errors later.

Using a Decision element, I am evaluating the sObject Collection Variable using Operator "Is Null" and the value: {!$GlobalConstant.True} or {!$GlobalConstant.False}.

When evaluting and sObject Collection Variable, the only operators available are: Equals, Not Equals, Is Null, Contains. When you choose Equals or Not Equal, you cannot leave the Value field blank to check for empty string, you have to provide another sObject Collection Variable to compare against. Also, the global constant "Empty String" is not available when evaluating an sObject Collection Variable so I believe i'm forced to use Is Null as the operator.

I use a screen with an Output Text to display the contents of the sObject Collection Variable and when it is empty (contains no record Ids), it displays "[]" (left and right square brackets) where if it were populated with record Ids, the Ids would appear within the brackets. So my guess is that sObject Collection Variable is not Null because the brackets are displayed in the output of an empty variable so my decision element is working correctly because the sObject Collection Variable is "not null".

Any other suggestions out there for evaluating sObject Collection Variable contents?

2
  • The results of a query is never null. You'd want to see if the list's size is zero. I'm not sure how to express that in a flow.
    – sfdcfox
    Oct 8, 2014 at 22:04
  • in Flow Builder, the below answer still holds: isNull doesn't work for collections. Assign a number variable equalsCount of the collection.
    – Foo Bar
    Sep 28, 2022 at 17:22

6 Answers 6

4

With Winter 18, Salesforce introduced a cool feature to grab the record count directly in the visual flow without the need to loop through it.

After a fast lookup,the record count can be easily fetched in a sobject collection variable and assigned to a number variable. Play around with the number variable directly. So finding out the number of open/closed/in progress opportunities gets more easy. Even you dont need to loop through the record set to get the count of records. You already have it before hand.

So for the above question, you can simply compare if the number variable is having null and decide what to do with it next.

In detail the design looks like this:

  1. Perform a fast lookup with your required filters enter image description here
  2. Assign the record count to a sObject collections variable. (in screenshot: nofCases)

  3. Use a Assignment step, to assign the variable to a number variable. (Point to note: the assignment variable should be of type number only to make use of equals count operator. In the screenshot, this is called testCount) enter image description here

  4. Voila! Now play around with the assigned variable :)

2

I ran into the same issue with an sObject collection variable being populated through a Visualforce page to include records selected from a list view (see Andy Fawcett's fantastic tutorial: https://andyinthecloud.com/2015/12/12/visual-flow-with-list-view-and-related-list-buttons/). The best solution, and the one Andy recommended, would probably be modifying the Visualforce page with an apex:pageMessage element to display the error before the flow is initiated when no records are selected. However, since I'm not a real developer, I ended up using the following approach (using this post on the developer exchange as a starting point: https://developer.salesforce.com/forums/?id=906F0000000BNOnIAO):

  1. Started the flow with a Loop element that looped through the sObject collection
  2. Added two standard variables (Data Type = Number, Scale = 0), called them Count and Total, and set the Default Value for Count to 0
  3. Connected the Loop to an Assignment element called Add to Count (in both directions, to loop through for each record in the collection), and set the assignment to add 1 to the Count variable for each loop
  4. Created another Assignment element called Total Count and connected it to the Loop as the next element to run at the End of Loop when the records in the collection have been exhausted. In this element I assigned the new value of the Count variable to the Total variable
  5. Added a Decision element to evaluate the Total variable with one Editable Outcome (No Records Selected) and a Default Outcome (Records Selected). If Total = 0, the Editable Outcome executes and an error "Return and make a selection" screen is displayed. If the Total is >0, the Default Outcome runs and a confirmation "are you sure you want to [execute flow actions] on [Total] records?" screen is displayed

A couple more steps in the flow, but it works great and having a total displayed on the confirmation screen is a plus!

1

How are you populating the Sobject Collection? If using a FAST Lookup make sure you check the box "Assign Null to the variable if no records are found". I have tested this successfully in my dev org using the following Fast Lookup and the decision works correctly. Fast Lookup

Decision

1
  • John - thank you for your comment. I agree that a Fast Lookup provides an option to store "null" and I've used this in other scenarios. I am populating this sObject Collection variable through a Visualforce page. The user selects 1 or more records from a related list, clicks a button which calls the VF page, the apex: repeat method is used in the VF page to capture the Ids of the selected records and then populate the collection and calling the Flow interview. It works great but I cannot evaluate if the user did not select records. Oct 14, 2014 at 16:21
0

It's hard to say for certain, but the brackets may in fact be appearing because that is the method in which all sObject Collection Variables are displayed. I do not believe the brackets are actually present in the data; if it was, the system would likely throw errors when it attempted to parse the data.

I see that you are using the word "guess". Have you actually tested the flow to see if "Is Null" "{!GlobalConstant.True} functions as needed? It has always worked for me and my needs.

4
  • 1
    Yes, I have tested. The variable is not null so the Decision element cannot evaluate the sObject Collection Variable using the "Is Null" operator. It seems the sObject Collection Variable is "empty" but the Decision element doesn't allow you to evaluate "emptyString". Oct 9, 2014 at 16:42
  • Could you create an empty variable and have it checkbook if it's equal to that variable? Oct 9, 2014 at 17:06
  • Within a Flow Decision element, you can only compare an sObject Collection Variable to "isNull" or to another sObject Collection Variable. You cannot compare to a normal variable or element. Oct 9, 2014 at 17:55
  • So an empty sObject Collection Variable? Oct 9, 2014 at 17:56
0

Found this issue myself with a normal collection variable. Looping probably wasn't an option at the time of this Q.

I created a boolean variable (preset to false) and a loop and then set the boolean variable to true in the loop. I could then check this variable to see if it was set. Maybe not the most efficient way if you didn't need to loop through your collection but it works.

0

Did you ever find out how to do this? I'm stuck in the same place except I'm trying to evaluate whether or not a normal collection variable is "null" (nothing in it) or "not null" (something is in it).

I was trying the suggestion from collymitch but even with a loop, it STILL wants to say that something is in the collection variable, even when I know it is blank.

Whenever a collection variable is null, it returns this: []

I'm at a complete loss! Thanks for any help!!

1
  • 3
    Actually I just figured it out here: GlobalConstant.EmptyString is how you say “Null” in Visual Workflow
    – John Braun
    Jan 26, 2015 at 0:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .