Update an item on a board

Here’s a failed attempt to update an item on one of my boards:
{ “mutation” : “{ change_column_value (board_id: XXXXXXXXX, item_id: YYYYYYYYY, column_id: “text21”, value: “SOMETHING”) { id }} )” }

and another:
{ ““mutation”” : “”{ change_column_value (board_id: XXXXXXXXX, item_id: YYYYYYYYY, column_id: “"text21"”, value: “"SOMETHING"”) { id }} )“” }

Can anyone see where I’m in error? Thanks.

1 Like

Hey @enlightened ,

check this out.

Greetings

2 Likes

@enlightened ya I encountered the same issue. Answer is to use variables! Please see the post that @TMNXT-Dev referenced & follow :hot_pepper: 's solution. That should solve your problem :+1:

2 Likes

Still in a hole! This should work:
{ “query” : “mutation change_column($value: “SAMPLE”) {change_column_value(board_id:218918653, item_id:551681223, column_id: “text21”, value: $value) {name} }” }

But no luck. It’s crazy to have to take pot-shot’s in order to make something work. Isn’t the ANY decent documentation on how to use this stuff? The examples are horrible, not real world. What can be done about this?

what the… I can’t get this working either… :question::question::question:

I tried both with & without variable:


w/ variable   :-1:

  const query2 = `mutation change_column_value($value: String!) {
      change_column_value (board_id: 726291451, item_id: 726291453, column_id: "name1", value: $value) {id}}`

  monday.api(query2, {variables: {"value": "SAMPLE"}})

w/o variable   :-1:

  const query3 = `mutation change_column_value (board_id: 726291451, item_id: 726291453, column_id: "name1", value: "SAMPLE") {id}}`
  
  monday.api(query3)

Variable works when changing value in a status column. But how do we change the string value for an item in a text column? There has to be a way for such a common use case :thinking:

Could there be another form we’re not aware of? Like a text JSON str equivalent of status… something like JSON.stringify({“text” : “sample”}) to pass in, even though that doesn’t rly even make sense…

^ don’t try that btw, it doesn’t work

idk hopefully someone else can chime in :wind_chime:

This should do the job

static async changeColumnValue(token, boardId, itemId, columnId, value) {

try {
  const mondayClient = initMondayClient({ token });
  const query = `mutation change_column_value($boardId: Int!, $itemId: Int!, $columnId: String!, $value: JSON!) {
    change_column_value(board_id: $boardId, item_id: $itemId, column_id: $columnId, value: $value) {
      id
    }
  }
  `;

  const variables = { boardId, columnId, itemId, value };
  const response = await mondayClient.api(query, { variables });

  if (response.errors) {

    const errorMessage = response.errors[0].message
      ? "AccountId: " + response.account_id + " Error: " + response.errors[0].message
      : response.errors[0];
    throw new Error(errorMessage);
  }
  return response;
} catch (err) {
  console.log(err);
}

}

I have this in a file called services/monday-service.js. In the calling function I do:

  • for text: changeColumnValue(token, boardId, itemId, columnId, JSON.stringify(autoId));
  • for status: changeColumnValue(token, parseInt(boardId), parseInt(event.pulseId), columnId, JSON.stringify({ index: onValue }));
1 Like

Fantastic solution by @basdebruin!

Your query was almost correct @supernova, JSON object have to be escaped by either using the JSON stringyfy method or format them yourself:

{variables: {\"value\": \"SAMPLE\"}}

Greetings

hey @TMNXT-Dev

I still can’t get this stringify thing to work.

In addition to your suggestion, I’ve tried each of these variations but to no avail:
variables: "{\"value\": \"SAMPLE\"}"
variables: JSON.stringify({"value": "SAMPLE"})


What I don't understand is,

if updating value of an item in a status column is this:    
"value": "{\"index\": 1}"

then how come updating value of item in a text column is this:
{variables: { jsonStr }}

shouldn’t it look something like this:

const jsonStr = JSON.stringify({someKey: someValue})
variables: { "value": jsonStr }

The inconsistent formatting doesn’t make sense to me

Hi, thanks for posting your solution @basdebruin

It looks promising, but I’m still confused. What are you passing in as the value for text? I see that you set $value var type to JSON instead of String. It’d be especially helpful if you can give us an example value for autoId that would work in this case?

hi @supernova

In the example given autoId is just a string. As the GraphQl expects a JSON I call my function with JSON.stingify(autoId).

That does make sense?

2 Likes

Shouldn’t we be able to just set String type directly then like $value: String! instead of $value: JSON! :thinking: though I did try that & found that it didn’t work…

wow it worked finally jeez thank you @basdebruin :+1: :1st_place_medal: :trophy:

@enlightened we were supposed to stringify a STRING to be passed in as the value. Setting $value as String! type or setting the value to a primitive string won’t work cause $value must be JSON, apparently

So try this:

const query = `mutation change_column_value($value: JSON!) {
       change_column_value (board_id: 218918653, item_id: 551681223, column_id: "text21", value: $value) {name}}`

await monday.api(query, {variables: {"value": JSON.stringify("SAMPLE")}})

Hey @supernova ,

sorry I should be more precise in such a case.
Let’s say we want to change a column value of a Long Text Column.
In the documentation, Column-Value-Section, you can see how your String has to be formated.

It would look like this:

const query2 = `mutation change_column_value($value: String!) {
      change_column_value (board_id: 726291451, item_id: 726291453, column_id: "long_text", value: $value) {id}}`;

const variables = "{\"text\" :\"Sample\"}";

monday.api(query2, { variables }) 

If you want to add a JSON and don’t want to format it by hand you need to pay attention to your input. It would look like this:

const query2 = `mutation change_column_value($value:JSON!) {
      change_column_value (board_id: 726291451, item_id: 726291453, column_id: "long_text", value: $value) {id}}`;

const variables = JSON.stringify({ 
        "text": "Sample"
 });

monday.api(query2, { variables })

Hope that helps.

2 Likes

Since I’m doing this from one of our web servers (IIS) I’m using a POST call like this:

set xmlhttp = server.Createobject(“MSXML2.ServerXMLHTTP”)
xmlhttp.Open “POST”,“https://api.monday.com/v2/”,false
xmlhttp.setRequestHeader “Authorization”, strToken
xmlhttp.setRequestHeader “Content-Type”, “application/json”
xmlhttp.setRequestHeader “Accept”, “application/json”
xmlhttp.send(str)

I’m still trying to figure out how to put a mutation with a variable in one string. I think this is correct:

str=“{ “query” : “mutation change_column_value($value: String!) {change_column_value(board_id:218933653, item_id:551622223, column_id: “text21”, value: $value) {name} }” }”

But I not sure where to put the value of the string variable. This DOES NOT work:
str=“{ “query” : “mutation change_column_value($value: “SAMPLE”) {change_column_value(board_id:218933653, item_id:551622223, column_id: “text21”, value: $value) {name} }” }”

Hey @enlightened :wave:

I’m glad to join in on the help party here! I’ll have to take a further look with the team to see what might be the root cause for this behavior and I’ll get back to you as soon as I can with more pointers :slight_smile:

That said, I am literally speechless at the amount and quality of help that @supernova, @basdebruin and @TMNXT-Dev have provided here!! Thank you so much for being an active pillar of our community and for helping our users where they need more support the most.

-Alex

2 Likes

The others brought up a good point about stringifying your JSON and I really think that is the main problem as well. Since you’re using XMLHttpRequest rather than Monday’s API, the requirement is a bit different. Moving forward I recommend using JSON.stringify() than try to figure out where to put the slashes and quotes. insert whohastimeforthat.gif
I believe you wanted to use it all in one line and stick your variables inside the query and not use variables, but unfortunately that’s not possible with changing column values as @supernova and @TMNXT-Dev have pointed out.

I have it working for XMLHttpRequest here:

The query:

const query = `mutation changeValues($board_id: Int!, $item_id: Int, $column_id: String!, $value: JSON!) {
        change_column_value (board_id: $board_id, item_id: $item_id , column_id: $column_id, value: $value) {
          id
        } 
      }`

The variables:

//The JSON.stringify() your value way

const variables =  {"board_id": boardId, "item_id": itemId, "column_id": "status", "value": JSON.stringify({index: 2})}

//Stringify the value yourself way:

const variables =  {"board_id": boardId, "item_id": itemId, "column_id": "status", "value": "{\"index\": 2}"}
      

I’m not familiar with IIS, but I am a bit familiar with XMLHttpRequests as I use fetch more frequently, so I hope you can translate it to how you want, but this is how I have it working:


      const xmlhttp = new XMLHttpRequest();
      const theUrl = "https://api.monday.com/v2"

      xmlhttp.open('POST', theUrl);
      xmlhttp.setRequestHeader("Content-Type", "application/json")
      xmlhttp.setRequestHeader("Authorization", token)

      //Yep, stringify it again.
      xmlhttp.send(JSON.stringify({query, variables}))

Hope that helps you get pass this issue! If not, let us know.

2 Likes

Hmm…

This works great via the https://monday.com/developers/v2/try-it-yourself:

mutation {change_column_value(board_id:XXXXXXXXX, item_id:YYYYYYYYY, column_id: “text21”, value: “"some text"” ) {id name}}

So it stands to reason this exact string (str) should work via POST to

set xmlhttp = server.Createobject(“MSXML2.ServerXMLHTTP”)
xmlhttp.Open “POST”,“https://api.monday.com/v2/”,false
xmlhttp.setRequestHeader “Authorization”, strToken
xmlhttp.setRequestHeader “Content-Type”, “application/json”
xmlhttp.setRequestHeader “Accept”, “application/json”
xmlhttp.send(str) or xmlhttp.send(JSON.stringify(str))

Neither work. And I’m not sure why using variables would have any effect if this string works fine inside the try it yourself area.

Hello @enlightened
I believe it would have to be

xmlhttp.send(JSON.stringify({query: str}))

as str is your query, correct? I should have made that clearer.

As for variables. You’re right and as discussed in @supernova’s post earlier: Can't change column value(s) Unfortunately the only way to get it working is with variables. May be a bug or if it’s not, the documentation should stress it more that variable is required. nudge @AlexSavchuk nudge

So if you’re going to add variables, you’ll just need to send it this way:

xmlhttp.send(JSON.stringify({query: str, variables: yourVariables}))
2 Likes

If you’re open to suggestions @AlexSavchuk , here are some that I have for monday’s current API doc:

  • where Playground is first introduced, specify that just because a query runs successfully in the GraphQL editor does not mean that that same query will work in code ( also a good place to clearly state requirement of variables* here )

    *maybe also emphasize variables usage at relevant checkpoints throughout the doc for those who don’t attentively read from A-Z… lol (me)

    anchor links or even a :warning: will do  :+1:


  • under   Mutating > Columns > Change column values :

    I suggest including another example for a simple Text column ( and/or add a section for   Text Column   to the list of column types under “Changing column values section” )

    cuz right now it’s not intuitive (to me at least) from what’s given that you have to convert a ~ string ~ to JSON for a Text column. Long Text + Status col inputs (status is the default example in the doc) scream Object! to a newbie. Since updating an item’s text is such a common use case, I feel like it deserves its own special place on the doc y’know?


  • include more concrete, practical, thorough examples

    something like this:

    const jsonStr = JSON.stringify(
      {
        text: "success", 
        status: {
          label: "Done"
        }
      });      
    
    const jsonStr2 = JSON.stringify(
      {
        text: "hooray!",
        status: {
          index: 1
        },
        date7: {
          date: "2020-09-10",
          time: "13:25:00"
        },
        email_1: {     
          email: "itsmyemail@mailserver.com",
          text: "my email"
        }, 
        link: {
          "url": "http://monday.com",
          "text": "go to monday!"
        },
        numbers_3: "110.95",
        long_text_1: {
          "text": "Hi, I'm a Long Text! I can contain up to 2000 chars! Text is my twin...what's her char limit, you ask? Idk! Sadly, our creators never told us ¯\_(ツ)_/¯ But hopefully we'll find out soon enough so we can let you know :p Anyway, try me out! Click add column > more columns & you'll find me there!"
        }           
      });
    

        reads better than this:

        {\"text\": \"success\", \"status\": {\"label\": \"Done\"}}


I know there's a section near the end laying out both forms for each individual col type (excluding text). That's great! But it'd be *even more* helpful, compact & comprehensible imo to have a complete example that demonstrates how to **Change multiple column values** as a quick one-stop reference point.

That way, whenever someone wants to update multiple column values together, s/he won’t have to jump to another section to collect the info needed. It’d be clear right off the bat which key/value pairs & types are expected for which column types.


Also, ( somewhat minor but still relevant I guess ) :

For argument types where we see !Int , !String , !JSON, the exclamation points are there to indicate that those args are required, right? Except at first sight, it reads the opposite! I’m sure this one’s a typo & ! should be at the end. Just interesting that a misplacement of 1 char can completely flip the intended meaning haha again, nbd since it’s easy to deduce from the given context.

Still, I think it’s worth pointing out since I do recall being confused about this very early on. And if someone misinterprets !JSON to mean that JSON is optional…well then you can see the trouble there. That alone may throw them off when their query doesn’t run in code as they expect it to.


TL;DR

emphasize variables requirement; add text column example; the more examples the merrier :partying_face: :balloon:


Just my 2¢ :sparkles:
:slightly_smiling_face:

2 Likes

Got fired up by your post & drafted up a whole proposal :hot_pepper: @pepperaddict lol :joy:

2 Likes