Script Runner Connect - Script for Subitem read / Item Write not working

I am trying to update a column date value anytime any changes occur in any columns of the groups subitem. When I make any change it runs via the webhook “when any column changes.” And it is linked to the ScriptRunner. Same result API key or not.

import { UpdateSubitemColumnValueEvent } from ‘@sr-connect/monday/events’;
import Monday from “./api/monday”;

// Define the constants for the script
const WORKSPACE_ID = “Design Phase”;
const BOARD_ID = “Active Projects”;
const COLUMN_NAME = “Date Last”;

/**

  • 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 {
    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;
    }
    try {
    // Extract necessary parameters from the event
    const { parentItemId } = event.event;

     // Function to update the "Date Last" column for an item to today's date
     const updateDateLast = async (itemId: string) => {
         try {
             const today = new Date().toISOString().split('T')[0];
    
             // Construct the mutation query to update the "Date Last" column
             const query = `
                 mutation {
                     change_column_value (board_id: "${BOARD_ID}", item_id: "${itemId}", column_id: "${COLUMN_NAME}", value: "${today}") {
                         id
                     }
                 }
             `;
    
             // Execute the mutation query to update the column value
             const response = await fetch('https://api.monday.com/v2', {
                 method: 'POST',
                 headers: {
                     'Content-Type': 'application/json',
                     'Authorization': 'Bearer YOUR_API_KEY' 
                 },
                 body: JSON.stringify({ query })
             });
    
             const data = await response.json();
    
             console.log(`"Date Last" column updated for item ${itemId} to ${today}`);
         } catch (error) {
             console.error('Error updating column:', error);
         }
     };
    
     // Update the "Date Last" column for the affected item
     await updateDateLast(parentItemId);
    

    } catch (error) {
    console.error(‘Error handling subitem column updates:’, error);
    }
    }

Hello there @irish_535!

I am not sure I understand the use case. What would a “groups subitem” be?

And where are you finding an issue?

Sorry - all of the groups subitems with multiple subitems. Any time ‘any’ column updates update parent items date. Want to do this in one shot instead of multiple automations.

Hello again @irish_535,

What if you use a regular “When any subitem column changes” webhook and then when that endpoint is called, you take the ID of the changed subitem to get the parent item’s ID via API and then change it?

image

I like your logic, I will give this a run.

Sounds like a plan @irish_535 !