javascriptzapiermonday.com

Zapier Code Mode : Integrating Monday.com


I am building a custom integration in Zapier to integrate with Monday.com.

What should happen is the user will insert a project name as an input. The code will lookup all projects and create a dictionary (technically speaking an Object) of project names and their ids. This dictionary should enable the script to lookup the respective project_id of the project name.

Then there will be some code to insert an item to that project.

It currently works in that it is able to create the dictionary however it is seemingly unable to lookup the project id in the dictionary, and spits out an empty result.

My code:

//Create variable based on input from user
var project_name = bundle.inputData.project_id;

var dict = {};

//Look up all projects (id and name) in Monday.com
const options_ids = {
  url: "https://api.monday.com/v2",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
    Authorization: `Bearer ${bundle.authData.access_token}`,
  },
  params: {},
  body: {
    query: "{  boards (ids:811745658){id name items { id name}}}",
  },
};

// Create dictionary of all project names and ids in Monday.com
var p_ids = z.request(options_ids).then((response) => {
  response.throwForStatus();
  const results = z.JSON.parse(response.content);
  var items = results.data.boards[0]["items"];
  for (i = 0; i < items.length; i++) {
    var key = items[i]["name"];
    var value = items[i]["id"];
    dict[key] = value;
  }
  return dict;
});

//Get the project ID of the project name that was entered as an input for use below
const project_id = p_ids[project_name];

const options = {
  url: "https://api.monday.com/v2",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
    Authorization: `Bearer ${bundle.authData.access_token}`,
  },
  params: {},
  body: {
    // 'query': '{ boards (ids:811745658) {id name items {subitems {id name}} } }'
    query: "{code to insert item to Monday project based on the project_id}",
  },
};

return z.request(options).then((response) => {
  response.throwForStatus();
  items = response.json;
  const results = response.json;

  return results;
});


Solution

  • The problem is that your code after populating the dictionary (starting with const project_id) doesn't wait for the dictionary to be populated to run.

    Using await will help line it up:

    // Create variable based on input from user
    var project_name = bundle.inputData.project_id;
    
    // Look up all projects (id and name) in Monday.com
    const options_ids = {
      url: "https://api.monday.com/v2",
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
        Authorization: `Bearer ${bundle.authData.access_token}`,
      },
      params: {},
      body: {
        query: "{  boards (ids:811745658){id name items { id name}}}",
      },
    };
    
    const projectIdResponse = await z.request(options_ids);
    projectIdResponse.throwForStatus();
    
    const results = z.JSON.parse(response.content);
    var items = results.data.boards[0]["items"];
    
    // Create dictionary of all project names and ids in Monday.com
    var dict = {};
    for (i = 0; i < items.length; i++) {
      var key = items[i]["name"];
      var value = items[i]["id"];
      dict[key] = value;
    }
    
    // Get the project ID of the project name that was entered as an input for use below
    const project_id = p_ids[project_name];
    
    const options = {
      url: "https://api.monday.com/v2",
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
        Authorization: `Bearer ${bundle.authData.access_token}`,
      },
      params: {},
      body: {
        // 'query': '{ boards (ids:811745658) {id name items {subitems {id name}} } }'
        query: "{code to insert item to Monday project based on the project_id}",
      },
    };
    
    return z.request(options).then((response) => {
      response.throwForStatus();
      items = response.json;
      const results = response.json;
    
      return results;
    });
    

    I didn't change your code besides using the await operator, so you'll want to double check that it all works. Also, you'll need to make sure your perform method has the async statement before it.