Skip to main content

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

  • May 20, 2024
  • 5 replies
  • 176 views

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);
    }
    }

5 replies

Matias.Monday
Forum|alt.badge.img
  • monday.com Team Member
  • May 21, 2024

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?


  • Author
  • New Participant
  • May 22, 2024

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.


Matias.Monday
Forum|alt.badge.img
  • monday.com Team Member
  • May 22, 2024

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


  • Author
  • New Participant
  • May 23, 2024

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


Matias.Monday
Forum|alt.badge.img
  • monday.com Team Member
  • May 23, 2024

Sounds like a plan @irish_535 !