Create Multiple items at once

Do you have an example of sending multiple mutations in one request with or using a variable?
I’m trying to implement it via JS but it seems I can only use variables with a single mutation.

Like this.

Query:

mutation ($boardId: Int!, $itemName1: String!, $itemName2: String!) {
  createItem1: create_item(board_id: $boardId, item_name: $itemName1) {
    id
    name
  }
  createItem2: create_item(board_id: $boardId, item_name: $itemName2) {
    id
    name
  }
}

variables:

{
  "boardId": 22222,
  "itemName1": "This is a new Item 1",
  "itemName2": "This is a new Item 2"
}

I hope this helps!

1 Like

Thank you very much!
This is really helpful. :slight_smile:

2 Likes

Hi is there any limit of mutations I can make at once? I need to update all items on a board, it means more than 400 rows.

Hello @frartur and welcome to the community!

I hope you like it here :muscle:

We do not recommend running multiple mutations simultaneously in the same object (board or item) so in this case, the best way to do this would be to do it sequentially.

I hope that helps!

Cheers,
Matias

Is there a reason this isn’t recommended - beside the problems related to complexity limit and retrying a partial query? I have often done a single call to the API with both change multiple column values and move item to group together for one item to try to reduce the delay between events that the user sees (so they dont seen values change then another long delay before the item moves).

Hello @codyfrisch!

Performing multiple mutations on the same object simultaneously (specially if the amount of mutations is a big one) may result sometimes in issues with one or more of those mutations.

Hi Team,

I went through the discussion above , but just wanted to check please if the request to create multiple items in a Single board in a single API call is beyond the scope of work still ?
We would have liked this feature as we try to load multiple items to each board (and have quite a few Boards in which we load data to ). Having this will help reduce our execution time.

Hello there @Avijit,

You can send multiple mutations in one HTTP request as explained here. A possible call could be:

mutation{
  createItem1: create_item(board_id: 1234567, item_name:"This is a new Item 1") {
    id
    name
  }
  createItem2: create_item(board_id: 1234567, item_name:"This is a new Item 2") {
    id
    name
  }
}

I hope that helps!

Cheers,
Matias

1 Like

Thanks @Matias.Monday, one other question could we even have this to send to multiple boards altogether. As for each create_item we pass the board_id so if we generate the entire mutation query for say 100 entries across multiple boards and items could it be done in a single HTTP request?

Is there any limit on the number of ‘create_item’ calls?

Hello again @Avijit,

Can you create different mutations for different boards in the same request?

Yes. You can.

Is there a limit for the number of mutations you can send together?

I will check if there is a specific limit regarding the amount of mutations and let you know

Cheers,
Matias

1 Like

Hello again,

There is no hard limit, but we do not recommend sending more than 10-20, as response time will be slow in that case.

Let me know if you have any other questions!

Cheers,
Matias

1 Like

Thanks Matias will limit to a batch of 10 as a convention then.

Glad to help here!

How can we structure a request to create multiple items with column values from variables using a For loop?

I can’t seem to figure this out.

Hello there @geeemaaan,

Here is an example:

app.post("/myendpoint", function(req, res) {

	 res.status(200).send(req.body)	

	 let itemNamesArray = ["Item10", "Item11", "Item12", "Item13"]

for (let i = 0; i < itemNamesArray.length; i++) {
		let query5 = `mutation { create_item (board_id:1234567890, item_name:${itemNamesArray[i]}, column_values:\"{\\\"text\\\":\\\"Some text\\\"}\") { id } }`;
	 
		fetch ("https://api.monday.com/v2", {
		  method: 'post',
		  headers: {
			'Content-Type': 'application/json',
			'Authorization' : 'MYAPIKEYHERE'
		  },
		  body: JSON.stringify({
			'query' : query5
		  })
		}).then(res => res.json())
		.then(res => console.log(JSON.stringify(res, null, 2)));
	}
})

Here I am passing the name of the items as a variable, but in the same way, you could pass somehting like the column values.

I hope that helps!

Cheers,
Matias

But that solution is making 4 separate posts to the API…?

I am trying to make this happen with one post…

I was trying to figure out how to format it sending variables in a separate array, but am now having more luck structuring the entire thing without using [variables]

Hello again,

Oh, my bad.

You can use a request like this one:

mutation{
  createItem1: create_item(board_id: 1234567, item_name:"This is a new Item 1") {
    id
    name
  }
  createItem2: create_item(board_id: 1234567, item_name:"This is a new Item 2") {
    id
    name
  }
}

And in each mutation, you can pass different variables.

Is that what you needed?

Hi @Matias.Monday

Apologies for not coming back to you :slight_smile:

What I meant by the order is the order of the items in the table once they are created. So I need the items to be created in the same order as I send them:

  • item 1
  • item 2

From my experiments, if I send the items in the same mutation, the order is not maintained, so if I send these items in this order:

  • Item 1
  • Item 2
  • Item 3

I can get a result in the table like this:

  • Item 2
  • Item 3
  • Item 1

So, If I send single mutations with one element each and wait until each mutation is finished before sending the next one, then the order is kept, but is painfully slow.

Hope I had explained myself this time, please let me know if you need more info.

Thanks,
Carlos

Regarding ordering items, you can add a “number” column and sort it by that column. Autopopulate the values to be 1, 2, 3 depending on the order you want it to be.