6

I have (amongst others) the following four functions.

  • fallback()
  • newSubmission()
  • installSubmissionTrigger()
  • uninstallSubmissionTrigger()

I have a trigger that:

  1. Runs on form submission.
  2. Calls fallback() that posts something to the Spreadsheet for review.
  3. fallback calls installSubmissionTrigger().
  4. installSubmissionTrigger creates a time-based trigger running every minute.
  5. The trigger calls newSubmission().
  6. newSubmission does something I want and calls uninstallSubmissionTrigger().
  7. uninstallSubmissionTrigger removes the time-based trigger.

All of this works fine using Rhino but when I enable V8 the time-based trigger becomes disabled for unknown reasons when it is supposed to run.

Also when using V8, if I run installSubmissionTrigger() manually, the trigger does fire.
If I run fallback() manually, the trigger also does fire.

What could be the unknown reason the trigger becomes disabled?

function fallback(event) {
  ...
  installSubmissionTrigger();
  ...
}

function newSubmission() {
  ...
  uninstallSubmissionTrigger();
  ...
}

function installSubmissionTrigger() {
  var properties = PropertiesService.getScriptProperties();
  if(!properties.getProperty("triggerID")) {
    var trigger = ScriptApp.newTrigger('newSubmission').timeBased().everyMinutes(1).create();
    properties.setProperty("triggerID", trigger.getUniqueId());
    Logger.log("Creating newSubmission trigger: " + trigger.getUniqueId());
  }
}

function uninstallSubmissionTrigger() {
  var properties = PropertiesService.getScriptProperties();
  properties.deleteProperty("triggerID");
  // Loop over all triggers.
  var allTriggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < allTriggers.length; i++) {
    // If the current trigger is the correct one, delete it.
    if (allTriggers[i].getHandlerFunction() === 'newSubmission') {
      ScriptApp.deleteTrigger(allTriggers[i]);
    }
  }
}

Use-case example:

  1. A customer submits a request for a pricing offer for new door.
  2. Then they also submit a request for a pricing offer an extension to their house.
  3. This door will most likely be part of the extension so ideally we would send this request to a company that deals with house extensions as well as doors.
  4. But if the door request was processed immediately it might have been sent to a specialist that exclusively deals with doors.
Rubén
  • 34,714
  • 9
  • 70
  • 166
Tharkon
  • 263
  • 1
  • 9
  • 2
    Why are you creating so many triggers? What are you ultimately trying to do? if you already have `fallback` on a form submit trigger, why not just call `newSubmission` from it? – IMTheNachoMan Feb 28 '20 at 16:41
  • @IMTheNachoMan Occasionally a single user might submit to the form more than once. If this is the case, the presence of the second submission might alter what happens to the first submission. It is an assumption that this second submission will happen within one minute. That is why `newSubmission()` is called one minute after the form is submitted. – Tharkon Mar 01 '20 at 03:50
  • Look into lockservice – TheMaster Mar 01 '20 at 07:02
  • 1
    So a second submission **may** be submitted and if it is it impacts the first one? Can you give an example? – IMTheNachoMan Mar 03 '20 at 12:43
  • Example: A customer submits a request for a pricing offer for new door. Then they also submit a request for a pricing offer an extension to their house. This door will most likely be part of the extension so ideally we would send this request to a company that deals with house extensions as well as doors. But if the door request was processed immediately it might have been sent to a specialist that exclusively deals with doors. – Tharkon Mar 03 '20 at 17:06

2 Answers2

4

This issue you're having has been reported and it's related with V8 runtime [1]. You could work with DEPRECATED_ES5 runtime version which is working as expected.

[1] https://issuetracker.google.com/issues/150756612

Andres Duarte
  • 3,166
  • 1
  • 7
  • 14
  • How long will the ES5 runtime remain available? Just want to make sure that we're not automaticalled moved over to V8 when ES5 is deprecated and my script stops working. – Tharkon Mar 04 '20 at 14:45
0

Here is how I overcame this issue. I added a doGet(e) function in my script and made my script a web app. Now if you setup a trigger from within doGet or doPost, your trigger won't get disabled. Atleast it did not get disabled for me as I tested.

Lastly, I used a free cronjob service and simply sent a GET request to my webapp [DEPLOYMENT] URL every Nth day of the week. Not an ideal solution but for a last resort, it is not bad.

Now, If you wish to setup a trigger from your own script after you have finished working then simply make a get request to webapp URL of your own script and it should work the same way.

Ismail
  • 634
  • 1
  • 5
  • 11