cancel
Showing results for 
Search instead for 
Did you mean: 

Add an ability to check if property exists in an object

It seems that MS Flow has no ability to properly check if an object exists.

 

Let's have an example with an out-of-the-box Approval action:

 

body('Start_Approval')['comments']

If an approver does not leave a comment and simply approvers or rejects - our code that tries to read the comments will fail.

 

Worarounds that do not work:

  1. Using condition step with `empty(body('Start_Approval')['comments'])` Because it will still fail
  2. using equals null: equals(body('Start_Approval')['comments'], null) This will also faild
  3. using contains function: `if(contains(body('Start_single_approval'), '"comments": '), body('Start_single_approval')['comments'] ,'')` - Because word "comments" can appear in different properties and overall it's a bad practice

Terrible workardound:

- Using Try-catch-finally scopes 

 

Please, implement a proper function that will check if property of an object exists. Example:

 

containsKey(object,"property_name")

 

 

More info about the issue is here

Status: New
Comments
Advocate I
if(empty(body('ApprovalByAnyDirector')?['comments']), 'None', body('ApprovalByAnyDirector')?['comments'])

 

This does the trick. Notice the question mark.

Kudo Commander

Thank you very much! (for posting and coming up with the ? solution)

Regular Visitor

I know this is old but I ran into this issue today and found this thread. If you can accept a null value as an output and proceed with the flow you only need to add the ? to silently continue and accept that there is a non-existing property and get a null value in return 

 

Example in a compose:

body('Start_Approval')['comments'] (not working if comments are missing)

body('Start_Approval')?['comments'] (the compose will be empty if the comments are missing)