Count of records returned

I've built a data action performing a contact lookup for ServiceNow. What I'd like to do is count the number records returned so I know if I had a single or multiple match.

My returned Data looks like this:

{
 "records": [
    {
      "name": "personA"
    },
    {
      "name": "personB"
    },
    {
      "name": "personC"
    }
  ]
}

My current work around is to look for a 2nd record and have a translationMapDefault value of "No" if there isn't one.

In my Architect flow I check if contact_two='No' then I know it was single match

{
  "translationMap": {
    "rawName": "$.records[0].name",
    "rawContact_two": "$.records[1].name"
  },
  "translationMapDefaults": {
    "rawContact_two": "\"No\""
  },
    "successTemplate": "{\n \"name\": ${rawName}\n, \"contact_two\": ${rawContact_two}\n }"
}

But I was hoping there was a more elegant way of achieving this by just counting the records and returning the count value

Thanks

Hello,

I didn't remember or didn't know...
It appears that the size() function can be used on Arrays (in the Data Action Response Configuration).
It can count the number of elements/objects in the array.

I mean something like this:

{
  "translationMap": {
    "rawName": "$.records[0].name",
    "rawContactCount": "$.records.size()"
  },
  "translationMapDefaults": {
    "rawContactCount": "0"
  },
    "successTemplate": "{\n \"name\": ${rawName}\n, \"contact_count\": ${rawContactCount}\n }"
}

With contact_count being defined as an Integer in your Data Action Output Contract.

Regards,

Jerome,

That's excellent. Thank you.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.