cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
LaurenceL
New Member

Check triggerBody for existence of a property without crashing

Hi - how do I check for the existence of a field in the triggerBody? I cannot check for null or empty as this crashes if the property actually doesnt exist. These do not work @equals(triggerBody()['partnerId'], null) or @empty(triggerBody()['partnerId']) if "partnerId" doesnt exist. I need to check as my HTTP request can occasionally not send the required property.

 

Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
LaurenceL
New Member

this was solved by using the @contains function as in @contains(triggerBody(), 'partnerId')

View solution in original post

9 REPLIES 9
LaurenceL
New Member

this was solved by using the @contains function as in @contains(triggerBody(), 'partnerId')

DenisMolodtsov
Kudo Kingpin
Kudo Kingpin

It does not really work for me

 

This still fails because genius MS Flow tries to evaluate `body('Start_single_approval')['comments']` expression even if you wrap it with if(contains()) 

 

if(contains(body('Start_single_approval'), '"comments": '), body('Start_single_approval')['comments'] ,'')

 

I also have the same issue where the contains function doesn't work checking for the existance of a property in an array. Exactly the same logic (i.e. within an If statement).

DenisMolodtsov
Kudo Kingpin
Kudo Kingpin

I could not find anything that can properly check for properties to exist...

 

The only ugly workaround is to create 3 scopes:

  • Try  - try to access a property that might not exist. If it does not exist - we fail and go to the next step
  • Catch - Run only if the previous scope fails
  • Finally - Run in any case

 

Scopes.png

DenisMolodtsov
Kudo Kingpin
Kudo Kingpin

Created request for a function that check if property exists: https://powerusers.microsoft.com/t5/Flow-Ideas/Add-an-ability-to-check-if-property-exists-in-an-obje...

 

Please, upvote if you face the same problem.

DenisMolodtsov
Kudo Kingpin
Kudo Kingpin

I haven't tested it yet, but @xilef suggests using '?' approach. Seems promising to me:



if(empty(body('ApprovalByAnyDirector')?['comments']), 'None', body('ApprovalByAnyDirector')?['comments'])

  

Tried it...even tried with not()...does not work

Thanks @LaurenceL , exactly what I was looking for. 

contains(triggerBody(), 'thing') worked great for me. 

Anonymous
Not applicable

if(empty(body('ApprovalByAnyDirector')?['comments']), 'None', body('ApprovalByAnyDirector')?['comments'])

This expression is not foolproof solution, since it will return 'None' not only when there is no comments property, but also when comments value is empty string. But for some use cases it may be the most compact.

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Difinity Conference 2022

Difinity Conference 2022

Register today for two amazing days of learning, featuring intensive learning sessions across multiple tracks, led by engaging and dynamic experts.

European SharePoint Conference

European SharePoint Conference

The European SharePoint Conference returns live and in-person November 28-December 1 with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Users online (1,795)