Creating a new item only adds the items name but not the fields

so im using the monday api in my code and im trying to add new items to a board, well the item gets added but the other fields are empty

const mondaySdk = require("monday-sdk-js");
const mondayApiKey = require("./apiKeys");
const monday = mondaySdk();
monday.setToken(mondayApiKey);

 const userData = {
    Email: "lol@lol.com",
    level: {label: "Pro"},
  
  };

  
  const encodedUserData = JSON.stringify(userData).replace(/"/g, "\\\"");

  
  const mutation = `
  mutation {
    create_item (
      board_id: 12345,
      item_name: "New Item",
      column_values: "${encodedUserData}") {
      id
    }
  }
`;
  
  monday
      .api(mutation)
      .then((response) => res.send({response, encodedUserData}))
      .catch((error) => {
        console.error(error); 
        res.send(error);
      });

First thing, is you’re using the column title as the keys in your userData. You need to use the underlying column IDs. See https://support.monday.com/hc/en-us/articles/360000225709-Board-item-column-and-automation-or-integration-ID-s on how to find them in the UI. The API returns them as the id field from querying columns - but I suspect you don’t need to do that.

Lastly, I would highly recommend investigating using GraphQL variables. You pass your serialized userData (without the replace part) as a “variable” in the request. Using the variables negates the need to deal with all of the escaping properly, which can become a nightmare. Just JSON.stringify() your userData and use that string in the variables object. The GraphQL server just reads the value from the variables when processing rather than trying to substitute the string in as you’re doing, and avoids all the string manipulation stuff entirely.

here is an example:


const mondaySdk = require("monday-sdk-js");
const mondayApiKey = require("./apiKeys");
const monday = mondaySdk();
monday.setApiVersion("2023-10");
monday.setToken(mondayApiKey);

const userData = {
 [emailColumnId]: "lol@lol.com",
 [levelColumnId]: {label: "Pro"}
}

const variables = {
   boardId: "12335",
   itemName: "New Item",
   columnValues: JSON.stringify(userData)
},

const mutation = `
  mutation ($boardId: ID!, $itemName: String!, $columnValues: JSON!) {
    create_item (
      board_id: $boardId,
      item_name: $itemName,
      column_values: $columnValues) {
      id
    }
  }
`;

  monday
      .api(mutation, { variables }) // note the addition of { variables }
      .then((response) => res.send({response, encodedUserData}))
      .catch((error) => {
        console.error(error); 
        res.send(error);
      });

The query above uses ID for the $boardId type - which is an API 2023-10 and later change. I’m doing this because 2023-07 will be discontinued January 15th, and 2024-10 will be default (2023-10 will be deprecated but still work). Currently 2023-07 is default during the transition to 2023-10 which has considerable breaking changes.

2 Likes

thnak you very much! it works! :partying_face:

1 Like

Thank you @codyfrisch as usual!

1 Like