Checking for null, which means you are checking to see if there is a value associated with something that can contain values, like a variable or an object, is important when doing calculations or setting variables. Trying to use a null value to set a variable or do a calculation can cause the flow to fail and is a quite common error.

In the examples for this article, I’m going to use data from a SharePoint list and an Excel table, but the data really could be from any source. There are things to watch out for, especially when setting and checking variables.

You can’t set an integer variable, float variable, or boolean variable to something that is null.

You can set a string variable, object variable, or array variable to null, but after setting, if you check to see if that variable is equal to null, it will be false. You need to use the empty() expression to check for null after setting the variable. See this article on using the empty() expression.

I follow these rules when thinking about checking for null.

1.) Check for null before setting variables, not after.
2.) Use the empty() expression to check for null when dealing with strings, arrays or objects.

Example 1: Check for Null using a Condition Action with SharePoint Data

With SharePoint data, you can check for null using the condition action.

1.) In the first box, put the SharePoint field you want to check.
2.) In the second box put “is equal to”.
3.) In the third box, type in “null”.

This works for every type of field, text, number, choice, etc.

Example 2: Check for Null and Set Variable using Expressions with SharePoint Data

Here I’m using the Set Variable action. I’m going to use the if() and equals() expression. I will check if the SharePoint field Status is null and write the string ‘started’ if it’s not null and ‘not started’ if it’s null. The outputs(‘Get’)?[‘body/Status’] is from the Get action, which is getting an item from a SharePoint list, above the Set variable action.

if(equals(outputs(‘Get’)?[‘body/Status’],null),’Not Started’,’Started’)

Example 3: Setting a String, Object or Array Variable to Null and then Checking the Variable for Null

Once you set a string, object, or array, you can’t use the field equals null condition to see if that variable is null or not. You need to use the empty() expression. More on that here.

The below is an example. Here I check if a SharePoint field called Code is null. It is and the result of the condition is true. I then set a variable, StringVar, to that field Code. The StringVar is set to null. When I check to see if StringVar is null, the condition returns false, meaning it’s not null. So it’s important you don’t set a variable and then check for null, check for null first.

Here is the setup.

Here is the result.

Example 4: Checking for null with data from Excel

When you get data from an Excel file, the object returns values that are in string format. You can’t use the below to check for null, it will always come back false. You must use the empty() expression to check for null.

Using the empty() expression is equal to true, returns true, and is what you would expect. See more on empty() expression here.

Since everything returned from Excel is a string, you also must make sure to change the type to an integer, if you are setting an integer variable or doing a calculation.

Here is what happens if you try to set an integer variable. You get this error, “The variable ‘IntegerVar’ of type ‘Integer’ cannot be initialized or updated with value of type ‘String’. The variable ‘IntegerVar’ only supports values of types ‘Integer’.”

You have to use the int() expression to convert the data to an integer to set the variable.

You must be careful when using int() that the data you get is in fact be an integer and is not null, you can’t convert a word to an integer and you can’t convert null to an integer. You will get an error.

Was this helpful?

0 / 0