Template Content
About the template
About ScriptRunner Connect
What is ScriptRunner Connect?
Can I try it out for free?
Yes. ScriptRunner Connect comes with a forever free tier.
Can I customize the integration logic?
Absolutely. The main value proposition of ScriptRunner Connect is that you'll get full access to the code that is powering the integration, which means you can make any changes to the the integration logic yourself.
Can I change the integration to communicate with additional apps?
Yes. Since ScriptRunner Connect specializes in enabling complex integrations, you can easily change the integration logic to connect to as many additional apps as you need, no limitations.
What if I don't feel comfortable making changes to the code?
First you can try out our AI assistant which can help you understand what the code does, and also help you make changes to the code. Alternatively you can hire our professionals to make the changes you need or build new integrations from scratch.
Do I have to host it myself?
No. ScriptRunner Connect is a fully managed SaaS (Software-as-a-Service) product.
What about security?
ScriptRunner Connect is ISO 27001 and SOC 2 certified. Learn more about our security.
This template demonstrates how to build an automation in monday.com to execute a script without exceeding a certain rate limit, by tracking the complexity of the API calls, and pausing when the available tokens are below a configured threshold. The job in this template will archive all the items that are completed in a board, calculate their total cost and send a report of the job to a configured user.
Parameters
to match your workspace: import Monday from "./api/monday";
let itemsDoneCounter = 0;
/**
* This job will scan for all the items in a board and seach for items that are in a DONE status.
* For these items, it will calculate their total cost, and archive them.
*
* This job is aware of Monday.com rate limits and if needed it will pause to wait for the tokens to be reset.
*
* In the end, the job will send a notification to a specified user.
*/
export default async function (event: any, context: Context): Promise<void> {
const ITEM_FIELDS = {
cursor: true,
items: {
fields: {
id: true,
name: true,
column_values: {
fields: {
id: true,
value: true
},
fragments: {
StatusValue: {
value: true,
__typename: true,
is_done: true
}
}
}
}
}
}
const { BOARD_ID, MIN_AVAILABLE_TOKENS, COST_COLUMN_ID, USER_TO_NOTIFY_ID, ITEM_PAGE_SIZE } = context.environment.vars;
// Getting the first page
const initResp = await Monday.Board.ItemsPage.getItemsPage({
args: {
ids: [BOARD_ID]
},
fields: {
items_page: {
args: {
limit: ITEM_PAGE_SIZE
},
fields: ITEM_FIELDS
}
}
})
let totalCost: number = 0;
const board = initResp.data.boards[0];
totalCost += await processItemsBatch(board.items_page.items, COST_COLUMN_ID);
let currCursor = board.items_page.cursor;
while (currCursor) {
const nextPageResponse = await Monday.ItemsPage.getNextItemsPage({
args: {
cursor: currCursor,
limit: ITEM_PAGE_SIZE
},
fields: ITEM_FIELDS,
complexity: {
after: true,
reset_in_x_seconds: true,
}
})
currCursor = nextPageResponse.data.next_items_page.cursor;
totalCost += await processItemsBatch(nextPageResponse.data.next_items_page.items, COST_COLUMN_ID);
const availableTokens = nextPageResponse.data.complexity.after;
if (availableTokens < MIN_AVAILABLE_TOKENS) {
console.log(`Spending tokens too fast, will pause for ${nextPageResponse.data.complexity.reset_in_x_seconds} seconds, please wait`);
await wait(nextPageResponse.data.complexity.reset_in_x_seconds * 1000);
}
}
console.log(`Total cost this period was ${totalCost}`);
// Send a report of the job to a user
await Monday.Notification.createNotification({
args: {
user_id: USER_TO_NOTIFY_ID,
target_id: BOARD_ID,
target_type: 'Project',
text: `Done ${itemsDoneCounter} items this period. Total cost was ${totalCost}`
},
fields: {
text: true
}
})
console.log(`Items done: ${itemsDoneCounter}`);
}
/**
* iterating a page of items. For items that are done, add the total cost and archive them
*/
async function processItemsBatch(items, costColumnId): Promise<number> {
let totalCost: number = 0;
for (let item of items) {
let value = 0;
let isDone = false;
item.column_values.forEach(column => {
if (column.__typename == 'StatusValue') {
isDone = column.is_done;
}
if (column.id === costColumnId) {
try {
value = Number(column.value.replace(/['"]+/g, ''));
} catch {
value = 0;
}
}
})
if (isDone) {
totalCost += value;
await Monday.Item.archiveItem({
args: {
item_id: item.id
}, fields: {
id: true
}
})
itemsDoneCounter++;
}
}
return totalCost;
}
const wait = (n) => new Promise((resolve) => setTimeout(resolve, n));