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’)[‘_ita_location_value@OData.Community.Display.V1.FormattedValue’]

and the following syntax:

items(‘Apply_to_each’)?[‘_ita_location_value@OData.Community.Display.V1.FormattedValue’]

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’)?[‘_ita_location_value@OData.Community.Display.V1.FormattedValue’])

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’)), ‘_ita_location_value@OData.Community.Display.V1.FormattedValue’),  substring(items(‘Apply_to_each’)?[‘_ita_location_value@OData.Community.Display.V1.FormattedValue’], 0, 3), ”)

The same works fine with object variables, too.

And, once that is in place, my Flow starts working just fine:

image

One thought 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

Leave a Reply

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