Skip to main content

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

  • December 27, 2023
  • 3 replies
  • 268 views

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);
      });

3 replies

  • Participating Frequently
  • December 27, 2023

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.

graphql.org

  • Author
  • New Participant
  • December 28, 2023

thnak you very much! it works! 🥳


Matias.Monday
Forum|alt.badge.img
  • monday.com Team Member
  • December 28, 2023

Thank you @anon29275264 as usual!