Every now and then, I would run into this error:
What’s happening there is that I’m going through a list of records, and, occasionally, there is a field that’s not populated. So, in the example above, here is where it fails:
That substring function expects a value, but how do we check if the value is there?
I could add a condition:
But you can see how it still does not work:
I could check if the property is empty:
That does not help either:
How do we work around this?
It’s, actually, simple.
There is a difference between this syntax (which I used in the conditions above):
and the following syntax:
items(‘Apply_to_each’)?[‘[email protected]’]
And that question mark before the property name makes all the difference. When using such syntax, the expression will return “null” if the property does not exist. And, when there is no question mark, the expression will fail, since it’ll try to access that non-existent property.
There are, probably, different ways to use it in the expressions/conditions, but here is an example:
And now it works just fine:
With that in place, my condition works just fine when testing or empty value:
PS. For the record, BELOW IS AN OLDER VERSION OF THIS POST – IT WORKS, BUT IT’S NOT HOW IT SHOULD BE DONE – SEE ABOVE INSTEAD
It seems here is what works pretty well (though I’d think it’s a bit resource-intensive?):
- We can use string function to convert our object to string
- Then we can use “contains” on that string to check for the presence of the property in that object
- And we can add “if” to the expression, so that the property is there we’ll be doing something with it. And, if not, we’ll just use some default value
It becomes a relatively long expression in the end (I could have done it in C# much faster), but it seems to work:
if(contains(string(items(‘Apply_to_each’)), ‘[email protected]’), substring(items(‘Apply_to_each’)?[‘[email protected]’], 0, 3), ”)
The same works fine with object variables, too.
And, once that is in place, my Flow starts working just fine:
Great find!
This did help me to resolve my issue and saved me a lot of headache.
Tnx. Martin
Yes agree with Martin this saved lots (more) of frustration
I have mine in a loop and then condition if array length. never had a problem with testing small data but when tested for prod failed after went through loop and returned no array.
added ? and it works as expected
Thanks
Thanks a lot this was a saver for us
Just for others who want to use it in another loop make sure you add that name correctly
empty(items(‘Apply_to_each’)?[‘[email protected]’])
can be
empty(items(‘Apply_to_each__2’)?[‘[email protected]’])
or
empty(items(‘Apply_to_each__3’)?[‘[email protected]’])
depending on which ever loop you are in
Hi there, I was having a similar issue and the easiest way I found to deal with it was using “Configure run after”.
On the original task I included the variable that might not be there, on the next one I have a copy but without the variable and I only run it if the previous one fails.
Finally, the one after this I run both if the previous is successful or was skipped.
TASK A
obj = last(items(‘Apply_to_each_Virtual_Machine’))[‘runtimeId’]
TASK B (run after if TASK A failed)
obj = “-”
TASK C (run after TASK B if success or skip)
use obj
Thanks you helped me out bigtime.
thanks – worked perfectly!
Thanks Alex, this was helpful. Shame there doesn’t seem to be a way to use an ODATA filter query to only get SharePoint files without particular properties, as this would remove the need for the condition check.
Thanks 😀
Thanks, this is a life saver.