Mapping key: values pairs server side

Bubblers, Trust you are keeping safe.

I would like to return a dynamic list from a custom datatype called COUNTY. fields of interest are as follows:
{“countyName”: “Bomet”,
“countyCode”: 36,
“_id”: “1584356642721x337270496762897500”}
Kericho, Bomet, ) and countyCode formatted as string for use in a USSD app.

So far, I have used jsonNEST to do a search and return counties in JSON format for use on server side via API workflow. I need help to access values from JSON object and create a new list like this one:
35. Bomet, 36. Kericho, 30. Baringo

Kindly advise why below code is returning undefined object:

function(properties, context) {
//let counties = properties.county // returning an error (counties.map is not a function)
let counties = JSON.parse(properties.county)
function mydata(item, index) {

    return item.countyCode + ". " + item.countyName
}
let newArray = counties.map(mydata)

return {"res":newArray}

}

Please note:

  1. that the api thing(s) to json step is returning data as expected.
  2. below code is working as expected:

function(properties, context) {

let counties = [
{
    "countyName": "Bomet",
    "Created Date": "2020-03-16T11:04:02.721Z",
    "Created By": "1466016066140x901869150344282400",
    "Modified Date": "2020-03-21T15:50:33.342Z",
    "countyCode": 36,
    "regixName": "\\n36 Bomet;",
    "_id": "1584356642721x337270496762897500",
    "_type": "custom.county"
},
{
    "countyName": "Kericho",
    "Created Date": "2020-03-16T11:04:13.640Z",
    "Created By": "1466016066140x901869150344282400",
    "Modified Date": "2020-03-21T15:50:26.082Z",
    "countyCode": 35,
    "regixName": "\\n35 Kericho;",
    "_id": "1584356653640x232865469323405120",
    "_type": "custom.county"
},
{
    "countyName": "Baringo",
    "Created Date": "2020-03-16T11:04:55.969Z",
    "Created By": "1466016066140x901869150344282400",
    "Modified Date": "2020-03-21T15:50:17.849Z",
    "countyCode": 30,
    "regixName": "\\n30 Baringo;",
    "_id": "1584356695969x681668885218751100",
    "_type": "custom.county"
}

];

function mydata(item, index) {

    return item.countyCode + ". " + item.countyName
}
let newArray = counties.map(mydata)

return {"res":newArray}

}
My code works fine when i use a sample data from jsonNEST directly as shown above.

Please advise if there is an easier way of doing this. CC @

Hello guys, would you know if there is a way one can iterate over a list in backend workflow? Still stuck with above issue.

I dont seem to find a plugin that can return a list of modified custom data types from API containing multiple values for every row in a list.

CC @keith @copilot, @ZeroqodeSupport @Thimo @vini_brito

You can make the backend workflow reschedule itself, it’ll be a loop :slightly_smiling_face:

@timothymugendi I assume you are building a server side action plugin here? (This should have been posted in plugin builders forum.)

Your issue is that the result of Search for COUNTYs is nothing like your “counties” array of objects.

Search for COUNTYs returns to you a List object which you then have to go fetch via its get() function.

Since a COUNTY is a Thing, what that get() will return to you is an array of objects representing COUNTYs. These have two functions on them, listProperties() and get().

It’s using THOSE functions that you would resolve your COUNTY objects into some component field on a COUNTY.

None of the above is hard, but you do have to understand what I point out above.

But your plugin doesn’t need to do it this way.

Since all you want to do is concatenate countyCode and countyName, make the inputs to your plugin County Code (a list of numbers - call the field codes or something) and County Name (a list of texts - call the field names or something).

And then you don’t have the list decomposition rigamarole. You’ll just have normal JavaScript array of numbers and an array of strings. So now in your code you’ll have properties.codes is a numeric array and properties.names is an array of strings.

And then your return value (which seems to be configured as a list of texts, right?) would just be:

var result
result = properties.codes.map(code, i => code + ". " + properties.names[i])
return {"res": result}

Of course it’s up to you to ensure that your list of numbers and list of texts are the same length, unless you wanna put some error handling in there.

But of course they will be the same length as you’ll feed these fields with basically the same search:

Codes input will be Search for COUNTY’s countyCode
Names input will be Search for COUNTY’s countyName

Thank you for solutions @keith and @vini_brito. I will go with @vini_brito proposal for now. I can see a big risk with server timing out for remote servers and also the risk of overloading bubble servers though if i set too few seconds.

I guess eventually I will have to find time and try @keith method eventually.