How to loop through subitems' ids of an item of a board using API

Hello community,

I am pretty new to monday.com, and this is my first experience with GraphQL
I am using Javascript in my API to mutate my board, specifically:

My objective:
I have a board with a few summary columns and a button. Each of the items of that board has a bunch of subitems. The purpose of the button mentioned above is to trigger my javascript code that writes a number into each of the subitems.

What is given:
BoardID and item_id (as pulseId) or rather, which item I clicked the button on.
From here, I seemed to be able to get to the board’s subitems ID:

query{boards(ids:$boardID){columns(ids:"subitems"){settings_str}}}
followed by: let subID = JSON.parse(data_subs.data.boards[0].columns[0].settings_str).boardIds[0]

If there’s a better way to get to sub-board-id please let me know, but ok.
So far so good. but …

My issue:
I can’t find a way to traverse the tree, that is, how to go from the given elements to loop through the subitems of a specific item, getting their IDs. Once I have the id, I know how to
for each ID I run ā€˜change_column_value(…)’

Any recommendation is greatly appreciated.
Thanks in advance,

This is the sort of query used to get a list of subitems in the current API:

query {
  items(ids: 4639429787){
    id
    column_values(ids: "subitems") {
      value
    }
  }
}

The response, you will need to JSON.parse(data.items[0].column_values[0].value and then you have the array of {linkedPulseId: number} objects which you can iterate.

{
  "data": {
    "items": [
      {
        "id": "4639429787",
        "column_values": [
          {
            "value": "{\"changed_at\":\"2023-09-15 17:10:21 +0000\",\"linkedPulseIds\":[{\"linkedPulseId\":5170146748}]}"
          }
        ]
      }
    ]
  },
  "account_id": 10330910
}

alternatively:

query {
  items(ids: 4639429787){
    subitems {
      id
      board {
        id
      }
    }
    id
  }
}

returns:

{
  "data": {
    "items": [
      {
        "subitems": [
          {
            "id": "5170146748",
            "board": {
              "id": "4456254446"
            }
          },
          {
            "id": "5170178532",
            "board": {
              "id": "4456254446"
            }
          }
        ],
        "id": "4639429787"
      }
    ]
  },
  "account_id": 10330910
}
1 Like

Hello Cody, thank you so much for such detailed examples.
This is absolutely perfect, very clear on my original question.

Is it possible to know the position of a subitem, which is it in order? Because the array returned, seems like getting it in the order of addition, not how they are listed, and if I moved the subitems with the browser UI interface, the array doesn’t not update the order.

Warmest regards,

Sasha

1 Like

Unfortunately, item order is not something the API returns reliably. Esp. for subitems.

Its been a long standing feature request here… i may be partial to having this too, and in the new API I am hoping that they will implement it (version 2024-01 I hope. 2023-10 is already pretty set in stone) I don’t think its possible there in 2023-10 though we do have the ability to get items returned ordered by a column’s values. So for example if you have them by sorted by a date, then you’re gold - but if you rely on the dragged order then nothing yet.

1 Like

I just voted that feature up by one. Seemingly trivial feature to implement, and they have this item position already.
Thank you for all your detailed replies.

Regards,
Sasha

1 Like

Thank you @codyfrisch !!! It is awesome how you help others over here!

@szabelin of course let us know if you have any other questions :grin:

Cheers,
Matias

1 Like