4

In Logic Apps, I have an expression:

coalesce(triggerbody().data.job_id,triggerbody().resource_id,'error')

I basically want to get the first one that is not null, however one of these does not EXIST in the json payload. I get an error:

'The template language expression 'coalesce(triggerbody().data.job_id,triggerbody().resource_id,'error')' cannot be evaluated because property 'data' doesn't exist, available properties are 'transaction_id, event_type, event_time, resource, resource_id, account_id, resource_third_party_id, request_user_type, request_user_id'. Please see https://aka.ms/logicexpressions for usage details.'.

If data doesn't exist, that value should be "null" and resource_id used. Any ideas what the expression would look like to have that behaviour?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
user1048175
  • 1,100
  • 3
  • 12
  • 29

1 Answers1

3

The problem here is that you are trying to access to a property of a null element:

coalesce(triggerbody().data.job_id,triggerbody().resource_id,'error')

As triggerbody().data is null, Logic App can't evaluate triggerbody().data.job_id, you should check first if triggerbody().data is null.

felixmondelo
  • 1,324
  • 1
  • 10
  • 19
  • triggerbody().data doesn't exist in the payload deliberately- if it is not there, it needs to take from triggerbody().resource_id, but how do I do this.... I have tried if(equals(triggerbody().data, null), triggerbody().resource_id, triggerbody().data.job_id) but same error. Not sure how to check for undefined – user1048175 Mar 20 '18 at 10:22
  • 8
    Instead of use triggerbody().data, use triggerbody()?['data']. The question mark operator lets you reference null properties of an object without a runtime error. For example, you can use this expression to handle null trigger outputs: @coalesce(trigger().outputs?.body?.property1, 'my default value') – felixmondelo Mar 20 '18 at 10:39
  • Bingo. Thanks so much! – user1048175 Mar 20 '18 at 10:46