Roll up subitem value to parent in monday.com board when a subitem changes


Get Started

Not the template you're looking for? Browse more.

About the template


This template demonstrates how to build an automation in monday.com, so when a subitem's number column value changes, then how to recalculate its parent item number column value. Get started to learn more.

About ScriptRunner Connect


What is ScriptRunner Connect?

ScriptRunner Connect is an AI assisted code-first (JavaScript/TypeScript) integration platform (iPaaS) for building complex integrations and automations.

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.

Template Content


README

Scripts

TypeScriptOnMondayWhenAnySubitemColumnChanges
When any subitem column changes

README


๐Ÿ“‹ Overview

This template demonstrates how to build an automation in monday.com, so when a subitem's number column value changes, then how to recalculate its parent item number column value.

๐Ÿ–Š๏ธ Setup

  • Configure the API Connection and Event Listener by creating a connector for monday.com, or use a existing one.
  • Navigate to Parameters and change the ITEM_COLUMN_LABEL and SUBITEM_COLUMN_LABEL parameters to appropriate values for your instance.

๐Ÿš€ Usage

Update Number column in a subitem in a monday.com board, after successful event processing the value of an item's number column is updated automatically based on the combined values of that item's subitems.

API Connections


TypeScriptOnMondayWhenAnySubitemColumnChanges

import { UpdateSubitemColumnValueEvent } from '@sr-connect/monday/events';
import Monday from "./api/monday";

/**
 * Entry point to when "any subitem column changes" event is triggered
 *
 * @param event Object that holds When any subitem column changes event data
 * @param context Object that holds function invocation context data
 */
export default async function (event: UpdateSubitemColumnValueEvent, context: Context): Promise<void> {
    if (context.triggerType === 'MANUAL') {
        console.error('This script is designed to be triggered externally or manually from the Event Listener. Please consider using Event Listener Test Event Payload if you need to trigger this script manually.');
        return;
    }

    const { ITEM_COLUMN_LABEL, SUBITEM_COLUMN_LABEL } = context.environment.vars;

    // Extract necessary parameters from the event
    const { parentItemBoardId, parentItemId, columnTitle } = event.event;

    // Check if column changes is related to Subitem column
    if (columnTitle !== SUBITEM_COLUMN_LABEL) {
        // If not, then halt the script execution
        return;
    }

    // Check if necessary parameters are present in the event
    if (!parentItemBoardId) {
        throw Error("Parent Board ID not present in the event");
    }
    if (!parentItemId) {
        throw Error("Parent Pulse Item ID not present in the event");
    }

    // Get item and its subitems column values in monday
    const dataResponse = await Monday.Item.getItems({
        args: {
            ids: [parentItemId],
        },
        fields: {
            column_values: {
                fields: {
                    id: true,
                    column: {
                        fields: {
                            title: true,
                        },
                    },
                },
            },
            subitems: {
                fields: {
                    id: true,
                    column_values: {
                        fields: {
                            text: true,
                            column: {
                                fields: {
                                    title: true,
                                },
                            },
                        },
                    },
                },
            },
        },
    });

    // Find the item columns in the board
    const itemColumnId = dataResponse.data.items[0]?.column_values.map(c => ({ ...c })).find(c => c.column.title === ITEM_COLUMN_LABEL)?.id;
    // Check if the item Column is present in the board
    if (!itemColumnId) throw Error("Item column not found");

    // calculate the sum of the values for each subitem column value
    let total = dataResponse.data.items[0].subitems
        .map(subitem => Number(subitem.column_values.find(c => c.column.title === SUBITEM_COLUMN_LABEL).text))
        .reduce(((acc, subitem) => acc += subitem), 0);

    // Update item column value with the sum of each subitem column value
    await Monday.Column.changeColumnValue({
        args: {
            item_id: parentItemId,
            board_id: parentItemBoardId,
            column_id: itemColumnId,
            value: total.toString(),
        },
        fields: {
            id: true,
        },
    });
}
Documentation ยท Support ยท Suggestions & feature requests