1
votes

I am trying to programmatically add a trigger to a Google Apps Script - I have a function main() I want to schedule, which wraps another timebased trigger call using the ScriptApp.newTrigger().create() call like this

 function main(){
/*
* do stuff
*/

doScriptCallback();
}
 function doScriptCallback(){
  if(CONFIG.CALLBACK_SCRIPT_NAME != ''){
    try {
      ScriptApp.newTrigger(CONFIG.CALLBACK_SCRIPT_NAME)
      .timeBased()
      .after(5000)
      .create()
      Logger.log('Scheduled ' + CONFIG.CALLBACK_SCRIPT_NAME);
    } catch(e) {
      Logger.log(e);
    }
  }
}

running main() from the editor correctly runs doScriptCallback() and schedules the function in CONFIG.CALLBACK_SCRIPT_NAME

But if I schedule main() then the doScriptCallback only logs the message, but the function does not run.

Is this a restriction in Google Apps script?

1
Maybe I haven't fully understood your question; I tested this with CONFIG.CALLBACK_SCRIPT_NAME as main and this seems to work without issue. Where are you getting the value to insert into the trigger as the function name?Rafa Guillermo
CONFIG is just a custom object at the top of the page. CALLBACK_SCRIPT_NAME is an unrelated script that I want to schedule programmatically. Main() is scheduled from the UI to run daily. doScriptCallback() is just a wrapper for setting a schedule for the script name referenced in the config object.Sergei Trunov
Where are you setting CONFIG.CALLBACK_SCRIPT_NAME; is this inside the main() { //do stuff }?Rafa Guillermo
No it's a globally scoped variable sitting at the top of the code.Sergei Trunov

1 Answers

1
votes

This appears to be a bug!

There is already a report on Google's Issue Tracker which detail the same kind of behaviour:

Google does seem to know about this issue but if it's causing problems you can file your own bug about it here.

You can also hit the ☆ next to the issue number in the top left on the aforementioned pages which lets Google know more people are encountering this and so it is more likely to be seen to faster - it already appears that this is a commonly encountered issue.

Workaround:

In the mean time it appears you can disable V8 runtime in Apps Script, and this will run your triggers in the Rhino run time. The reports only affect V8, and I can confirm I was able to run the function created by the nested trigger in Rhino.

You can disable V8 by following the Run > Disable new Apps Script runtime powered by Chrome V8.

I hope this is helpful to you!