Create_item and column value

Hi,

I’m receiving a ton of issues creating an item with column_values. I’ve gotten both the HTTP 500 error, parse errors on the column_id and parse errors on “{” and etc.

Can someone kindly help with this, here’s the rough code:

function createItem(){
  var column = JSON.stringify({numbers:'88'});
  var mutation = 'mutation{create_item(board_id:my_board_id, group_id:my_group_id, item_name:testingname, column_values:{numbers: 88}) {id} }';
  var mutation1 = 'mutation{create_item(board_id:my_board_id, group_id:my_group_id, item_name:testingname, column_values:'+column+') {id} }';
  var response = UrlFetchApp.fetch("https://api.monday.com/v2", {
    method: 'post',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': apiKey
    },
    payload: JSON.stringify({
      'query': mutation1
    }),
    muteHttpExceptions: true
  })
  Logger.log(JSON.stringify({'query': mutation}))
  var json = response.getContentText()
  var returndata = JSON.parse(json)
  Logger.log(returndata)
}

Using mutation1 returns me “Parse error on “numbers” (STRING) at [1, 103]”
While using mutation returns me “Internal server error”

And “numbers” is the id for the column within the board that I would like to change as an example. The create_item works fine without the column_value input.

Hey @yuhaocooper

I’d love to help with this!

FIrst off, it doesn’t seem like you are sending your request with GraphQL Variables, but rather simply inputting the values from your code into the query. Is my understanding correct?

If you are not sending the Item Name as part of your variables, you will also need to properly escape it. Let me know if the following example makes sense;

const fetch = require('node-fetch');

let query4 = 'mutation ($myItemName: String!, $columnValues: JSON!, $boardId: Int!) { create_item (board_id:$boardId, item_name:$myItemName, column_values: $columnValues) { id } }';
let vars = {
  "myItemName" : "Hello, world!",
  "columnValues": "{\"numbers\":88}",
  "boardId": 1199874661
};

fetch ("https://api.monday.com/v2", {
  method: 'post',
  headers: {
    'Content-Type': 'application/json',
    'Authorization' : 'ReplaceWithYourAPIKey'
   },
   body:  JSON.stringify({
   'query': query4,
   'variables' : JSON.stringify(vars)
  })
})
   .then(res => res.json())
   .then(res => console.log(JSON.stringify(res, null, 2)));

This works well for me on my end. Notice the escaping in column values as well :slight_smile:

You can also avoid escaping by using JSON.stringify dirctly within your variables, and the nagain in making your request:

let vars = {
  "myItemName" : "Hello, world!",
  "columnValues": JSON.stringify({"numbers":88}),
  "boardId": 1199874661
};

   body:  JSON.stringify({
   'query': query4,
   'variables' : JSON.stringify(vars)

What do you think? Let me know.

-Alex

Hi @AlexSavchuk ,

Thanks for the examples! I’ve just changed my code to use the GraphQL variables and it is working on my end while following your formatting.

A separate issue I’m facing right now with the formatting of the GraphQL variable is with using the JSON.stringify directly on an array variable. E.g:

var personids = [{“id”:16200593,“kind”:“person”}, {“id”:9055423,“kind”:“person”}]

“columnValues”: JSON.stringify({“numbers”:num,“person”:{“personsAndTeams”:personids},“timeline”:{“from”:“2021-04-25”,“to”:“2021-04-27”}})

How should I format personids or the columnValues variable to make this work?

Best regards,
Yuhao

Hey @yuhaocooper

That’s a great question!

When wanting to assign multiple users to a People column using the API, you will need to send an array of User IDs, as well as the user kind (team/person). Here’s an example of how this query looks in the GraphQL playground:

mutation {
  change_column_value (board_id: 1244760373, item_id: 1244760382, column_id: "person", value: "{\"personsAndTeams\": [{\"id\":9603417,\"kind\":\"person\"}, {\"id\":14205740, \"kind\":\"person\"}]}") {
    id
  }
}

From wha it looks like, you might need to add a right curly bracket after the Numbers column value you are sending. I hope this helps!

-Alex