Question

Code by Zapier: how to move the top 5 cards from one list to the next list in Trello

  • 10 April 2023
  • 3 replies
  • 63 views

Hello,

Inside Trello I have one list named Buffer, this is the first list on the board. The Buffer list consists of around 15 cards. Every Sunday 5 cards from the top of the Buffer list are moved to the next list named Translate. This cannot be done via Trello’s built in automation tool. So I asked ChatGPT for a solution to automate this.

ChatGPT suggested to use Code by Zapier. After a few attempts to solve the errors, there were no errors anymore. But no cards were moved when starting the script.

This is the script:

const Trello = require("node-fetch");

const apiKey = "apikey";
const apiToken = "apitoken";
const boardId = "boardid"; // Replace "boardId" with the actual ID of your Trello board

// Fetch the first list on the board
Trello(`https://api.trello.com/1/boards/${boardId}/lists?key=${apiKey}&token=${apiToken}`)
.then(res => res.json())
.then(lists => {
const firstList = lists[0].id;
console.log(`First list ID: ${firstList}`);

// Fetch the cards in the first list
return Trello(`https://api.trello.com/1/lists/${firstList}/cards?key=${apiKey}&token=${apiToken}`);
})
.then(res => res.json())
.then(cards => {
console.log(`Moving ${cards.length} cards to the next list...`);

// Move the first 5 cards to the next list
const cardsToMove = cards.slice(0, 5);
const nextList = cards[0].idList;
const movePromises = cardsToMove.map(card => {
return Trello(`https://api.trello.com/1/cards/${card.id}?idList=${nextList}&key=${apiKey}&token=${apiToken}`, {
method: "PUT"
});
});

// Wait for all cards to be moved before logging a message
return Promise.all(movePromises);
})
.then(() => {
console.log("Cards moved successfully!");
})
.catch(err => console.error(err));

// Your Code by Zapier code goes here

// Define the output object
const output = {
id: 1234,
message: "Card moved successfully!"
};

// Return the output object
callback(null, output);

 

The automation will be something like this:

  • Inside Trello Butler automations will start every Sunday. It will post to url with payload "board id" and "numberofcardstomove". Because it will run on multiple boards. And the number of cards to move can be different.
  • Inside Zapier the webhook will be the trigger.
  • After this Code by Zapier starts, and this code will move x cards from the first list to the second list on the board.

Some questions:

  1. Does someone know if this is even possible to do with Code by Zapier?
  2. Where to store the api key and api token?
  3. Why is the script not working? Code by Zapier does not give any errors, but it's not working.

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

3 replies

Userlevel 7
Badge +14

Hi @mbbz 

Good question.

Using the Code step in a Zap is an advanced app.

If you need help, consider hiring a Certified Zapier Expert: https://zapier.com/experts/automation-ace

Hello @Troy Tessalone 

Do you think it is even possible to do this with Code by Zapier? To get the first 5 or 7 cards from the top of the first list, and move those 5 or 7 cards to the next list on the same board?

Userlevel 7
Badge +14

Hi @mbbz 

Yes, it should be possible with the Trello API endpoints: https://developer.atlassian.com/cloud/trello/rest/api-group-lists/

 

  1. Get Cards in a List
  2. Code to get top 5
  3. Update a Card