Tip #1256: Get attribute of the first record from the list

This is probably a super simple question, but I’m a super simple flow user. After listing records from D365 how would I go about retrieving an attribute value of the first result returned?

Michael “Super Simple Flow User” Ochs

I won’t name the person who suggested the following:

  1. Declare variables to store values.
  2. Include top 1 limit for your query.
  3. Loop through your set (so 0 or 1 record) and set required values to variables.
  4. Use it and enjoy.

You know who you are, Andrii “Granny’s Moonshine” Butenko, and credit is where credit due – step 2 is actually correct if you only need that one record. I guess step 4 is also correct when you get something right.

The way I would do it:

  1. Build the query using filter, orderby and top to get exactly what I am after.
  2. Use expression to get the attribute while avoiding the runtime errors.

Demo

Let’s say I have an account and I want to find the full name of child contact created last.

Build the query by filtering contacts on the parent account, ordering the results by createdon in descending order (so that the last created contact is on top), then limiting results to 1.


After that, use the following expression to get the full name of the first contact returned.

body('Get_Last_Contact')?['value']?[0]?.fullname

Note that the query may or may not return any results so we use question mark operator (?) to avoid runtime errors.

To reference null properties in an object without a runtime error, use the question mark operator.

Workflow Definition Language schema

The result is either the full name or empty (if account has no contacts).

The only downside of using the expression (besides being not-exactly-nocode) is that the expression won’t know when you rename the List Records step. Which is why Tip #1247 is important.

Cover photo by unsplash-logoCupcake Media