Alternative approach to check for a single Dynamics record in Flow

[Update 17/20/2019]
Note that you need to handle null checks and the condition should be updated as follows
contains(coalesce(body('List_records')?['value']?[0]?['ItemInternalId'],'NULL'),'-')
This uses the Coalesce function, which returns the first non null value in the parameters. Thanks Megan for bringing up the bug!

Microsoft Flow has been a revelation when it comes to empowering the 'Citizen Developer'. The range of connectors and its simple configuration UI has led to this uptake. It has a range of Dynamics connectors too! One item that irks me is that to query/'get' a single record from Dynamics, based on values other than the GUID, you have to use the 'List records' action. The 'Get record' action requires a GUID value to be provided:

alt

In the scenario where you have a unique property on a record, you can build the Odata query to execute e.g getting a contact by a specific license ID

alt

My annoyance is that you then have to iterate the results through a for loop via the 'Apply to each' action. What if you wanted to have logic or a different process to execute dependent on whether or not the record was found? Sure you could initialize a variable at the start of the Flow and update the variable in the 'Apply to each' and then have logic based of that variable value. Whilst that could work, I feel that the Flow starts to get 'busy' or messy when multiple queries back to Dynamics are needed.

An alternative approach, which I have been using, is to use the 'Condition' action.
Each response back from Dynamics (when a record is found) comes back in a consistent format populated with the associated record meta-data. The key extract from the Json response below:

{ "@odata.context": "https://xxxxxxxxxxxx/$metadata#datasets('xxxxxxxxxxx')/tables('contacts')/items", "value": [ { "@odata.etag": "", "ItemInternalId": "c55c3b47-b07c-e711-8124-e0071b665271",

The item we should check for in our condition is the ItemInternalId. If it has a value, then then a record was found. This is how the Flow structure looks.

alt

The condition needs to be edited in advanced mode and the following content is logic it contains:

@contains(body('List_record')?['value'][0]?['ItemInternalId'],'-')

It looks to 'crawl' the result of the 'List__record' action execution and checks if there is a '-' in the ItemInternalId. The '?' is a null check so that when a record isn't found, the logic still executes correctly and defaults the value to blank.

Using the ItemInternalId means that this query will work across any Dynamics entities as it is part of the meta data of the record and not entity specific. So you can 'copy and paste' the query above and it will work in your context. Note that 'List___record' is the label of the List record action. If you renamed it, the reference in the query above would need to be changed too. The format is its the name of the action, with spaces replaced with the '_' character.

It took a lot longer to write and explain this blog than it does to actually implement this so I hope that it helps!