javascriptgoogle-sheetsgoogle-apps-scriptgoogle-cloud-platformtriggers

How to get current active/triggered trigger ID ,


I have build an Sheets Add-on Which performs a task based on trigger setup by user So the problem is that User sets triggers for multiple sheets e.g Sync

the trigger is executed but only for 1st trigger in the list of triggers not all

The PROBLEM IS only one trigger is being triggered for all of the setup triggers

Logs for one trigger & the same logs comes for all triggers

Active Trigger: -5962537404932988746 for function: fetchSynctriggerWithDetails
Active Trigger: -9139074510508922527 for function: fetchSynctriggerWithDetails
Active Trigger: -1495623190131824397 for function: fetchSynctriggerWithDetails
Active Trigger: -1224866675796805716 for function: fetchSynctriggerWithDetails
Active Trigger: 1983234386806141271 for function: fetchSynctriggerWithDetails
Active Trigger: -8082155554848723508 for function: fetchSynctriggerWithDetails

Response: {"success":true,"triggerDetails":{"trigger_id":"-5962537404932988746","sheet_name":"SKU","databaseid":"1","trigger_time":"1hour"}}

so the problem is it has all the triggers but it always run for

Response: {"success":true,"triggerDetails":{"trigger_id":"-5962537404932988746","sheet_name":"SKU","databaseid":"1","trigger_time":"1hour"}}

Because it can;t find var triggerId = currentTrigger.getUniqueId(); for each trigger & each time it returns -5962537404932988746 as current trigger ID

which is wrong

ANY HELP IS APPRECIATED


function createSyncTrigger() {

/// all the setup for setting up trigger 

// I am also saving trigger in PropertiesService

PropertiesService.getScriptProperties().setProperty(triggerId, JSON.stringify(triggerDetails));

}


Code snippet to fetch trigger details


function getTriggerDetailsSync() {
  var triggers = ScriptApp.getProjectTriggers();
   // Log all active triggers for debugging
  triggers.forEach(trigger => {
    Logger.log('Active Trigger: ' + trigger.getUniqueId() + ' for function: ' + trigger.getHandlerFunction());
  });

   //var currentTrigger = triggers.find(trigger => trigger.getHandlerFunction() === 'fetchSynctriggerWithDetails');

 // Get the current trigger that invoked this function
  var currentTrigger = ScriptApp.getProjectTriggers().find(trigger => 
    trigger.getHandlerFunction() === 'fetchSynctriggerWithDetails'
  );

  if (!currentTrigger) {
    Logger.log('No trigger found for fetchSynctriggerWithDetails');
    return null;
  }

  var triggerId = currentTrigger.getUniqueId();

}

Solution

  • I somehow fixed it using events

    // Func Called in the Trigger  
    // passing events to getTriggerDetailsimport func
    
    function fetchTableDataWithDetails(e) {
      var triggerDetails = getTriggerDetailsimport(e);
      
      if (!triggerDetails) {
        Logger.log('No trigger details found.');
        return;
      }
    
    }
    
    
    //USING EVENT TO GET CURRENT TRIGGER 
    
    function getTriggerDetailsimport(e) {
    
       if (!e || !e.triggerUid) {
        Logger.log('Error: No event object or triggerUid found.');
        return null;
       }
    
      var triggerId = e.triggerUid; // Get the unique ID of the active trigger
      Logger.log('Active Trigger ID: ' + triggerId);
    
    }
    
    
    

    & it works for each event of trigger i get correct trigger ID

    //LOGS for some trigger events Active Trigger ID: 1983234386806141271 Active Trigger ID: -8082155554848723508 Active Trigger ID: -5962537404932988746

    SO FINALLY IT WORKS