[SalesForce] Visual Workflow – how to evaluate whether an sObject Collection Variable isnull

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?

Best Answer

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 :)