Understanding “Record Count” and “NULL” condition check expression in “List Records” step of Common data service action in Microsoft flows

Be it “Citizen developers” or long time technical consultants in Dynamics, Microsoft Flows haven’t ceased to impress all. It’s liked by developers and customers alike and many a times have a deciding factor when choosing Dynamics 365 as the desired CRM platform over its competitors.

And if you have used Microsoft flows for Common data service, you must have used the “List records” step as well. And there are many cases where you need to perform an action based on whether the list returned any records or not. While this just seems too easy, surprisingly if you are not aware of the syntax, it may take some time for you to make it work.

The syntax for it is – length(body(‘<your list record action name>’)?[‘value’])

Well, the length function here is pretty obvious. However the trick here is to understand on how to get the array of items returned. If you inspect the results of list record step, you will find that the results are returned in an array called ‘value’.  You can check the length to be greater than zero or not and then take appropriate steps.

image

But wait. What is the “?” operator used for. And believe me, I keep on getting this question multiple times. To put in simple terms, it does check for not null condition and then evaluates the next step, just like you do in your code so that you don’t get “Object reference” error in your code.

Let’s take an example here. Say you want to get the GUID of the first record returned by “List Records” action. To test this in my flow, I create a very simple flow with “List records” step and post that I am initializing a variable with the GUID of the first record.

image

Observe carefully here. I have not used “?” operator here. Now this flow runs successfully when List records steps return data. However when there are no records returned in “List Record” step, then we get the below error.

image

I go ahead and update the expression now as shown in the below screenshot. Notice I have used the “?” operator here.

image

All set and done and when I run the flow this time, the flow runs successfully. As expected the value of the variable “X” is empty as there are no records returned.

image

Small stuff but end up saving some time.

Hope this helps!

Cheers!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

Multiselect picklist for Dynamics 365 (http://www.xrmforyou.com/multi-select-picklist.html)