Power Automate – how do we check if a property exists in the object?

By | December 20, 2020

Every now and then, I would run into this error:

image

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:

image

That substring function expects a value, but how do we check if the value is there?

I could add a condition:

image

But you can see how it still does not work:

image

I could check if the property is empty:

image

That does not help either:

image

How do we work around this?

It’s, actually, simple.

There is a difference between this syntax (which I used in the conditions above):

items(‘Apply_to_each’)[‘[email protected]’]

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:

empty(items(‘Apply_to_each’)?[‘[email protected]’])

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:

image

9 thoughts on “Power Automate – how do we check if a property exists in the object?

  1. Martin Swinkels

    Great find!

    This did help me to resolve my issue and saved me a lot of headache.

    Tnx. Martin

    Reply
  2. Crash

    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

    Reply
  3. Shehla

    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

    Reply
  4. Eduardo Russo

    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

    Reply
  5. Liam

    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.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *