Search code examples
google-apps-scriptgoogle-forms

Google Apps Script: "Script function not found" error


I have been searching around for a solution for my issue but have not been able to find one, and as a result am writing my first question in Stack Overflow.

I am currently writing a simple program on Google Apps Script that sends out an email reminder to a user if they forget to submit a Google Form by a certain date. Right now I have 3 different functions: onFormSubmit, scheduleTrigger, and reminderEmail. Below is the code I have written out thus far:

/**
This function searches the Google Form for the date when the previous
session was held & the date when the next session will be held. 
This function also calculates the date when to remind the user if they
forget to submit the Google Form. This reminder date is calculated to be 2 
days after the date when the next session is held. 
This function calls the next function: scheduleTrigger.
*/
function onFormSubmit(e) {
    var form = FormApp.getActiveForm();
    var frm = FormApp.getActiveForm().getItems();
    var futureDate = new Date(e.response.getResponseForItem(frm[3]).getResponse());
    var pastDate = new Date(e.response.getResponseForItem(frm[0]).getResponse());

    var reminderDate = new Date(futureDate.setDate(futureDate.getDate() + 2));
    futureDate.setDate(futureDate.getDate() - 2);

    scheduleTrigger(reminderDate, futureDate, pastDate, form);
}

/**
This function schedules the reminder email trigger at a specific date. The
specific date is the reminder date that was calculated in the previous function.    
This function calls the next function: reminderEmail. 
*/
function scheduleTrigger(reminderDate, futureDate, pastDate, form) {
    ScriptApp.newTrigger('reminderEmail(reminderDate, futureDate, pastDate, form)').timeBased().atDate(reminderDate.getFullYear(), reminderDate.getMonth(), reminderDate.getDate()).inTimezone('America/New_York').create();
}

/**
This function checks the submissions if the form has been submitted. 
If there is no submission, then it sends out an email reminder. 
*/
function reminderEmail(reminderDate, futureDate, pastDate, form) {
    var count = 0;
    var formResponses = form.getResponses();
    for (var i = 0; i < formResponses.length; i++) {
        var formResponse = formResponses[i];
        var itemResponses = formResponse.getItemResponses();
        for (var j = 0; j < itemResponses.length; j++) {
            var itemResponse = itemResponses[j];
            if (itemResponse == futureDate) {
                count++; 
            }
        }
    }

    if (count != 2) {
        MailApp.sendEmail("[email protected]",
                          "Submit Form Reminder",
                          "Hey! You didn't fill out the form yet!");
    };
}

The issue I am running into is that I receive an error message stating "The selected function cannot be found" for function reminderEmail whenever I run the program. I have looked around and followed previous posts made to solve this issue, such as renaming the function and restarting with a whole new different Google From but nothing has worked. I also have full ownership and authorization to the script so permissions shouldn't be an issue.

Please let me know if you have any ideas on how to solve this problem. Any and all feedback is welcome! If there is any part of my question that is unclear please let me know. Thank you for taking the time to read through this lengthy post.


Solution

  • The reason it's failing is because you need to only pass the function name to ScriptApp.newTrigger. You can't send parameters with it, that's something you'll have to handle in other ways.

    Documentation

    /**
    This function searches the Google Form for the date when the previous session was held & the date when the next session will be held. 
    This function also calculates the date when to remind the user if they forget to submit the Google Form. This reminder date is calculated to be 2 days after the date when the next session is held. 
    This function calls the next function: scheduleTrigger.
    */
    function onFormSubmit(e) {
      var form = FormApp.getActiveForm();
      var frm = FormApp.getActiveForm().getItems();
      var futureDate = new 
    Date(e.response.getResponseForItem(frm[3]).getResponse());
      var pastDate = new 
    Date(e.response.getResponseForItem(frm[0]).getResponse());
    
      var reminderDate = new Date(futureDate.setDate(futureDate.getDate() + 2));
      futureDate.setDate(futureDate.getDate() - 2);
    
      scheduleTrigger(reminderDate);
    }
    
    /**
    This function schedules the reminder email trigger at a specific date. The specific date is the reminder date that was calculated in the previous function.    
    This function calls the next function: reminderEmail. 
    */
    function scheduleTrigger(reminderDate) {
      ScriptApp.newTrigger('reminderEmail').timeBased().atDate(reminderDate.getFullYear(), reminderDate.getMonth(), reminderDate.getDate()).inTimezone('America/New_York').create();
    }
    
    /**
    This function checks the submissions if the form has been submitted. If there is no submission, then it sends out an email reminder. 
    */
    function reminderEmail() {
      var form = FormApp.getActiveForm();
      var count = 0;
      var formResponses = form.getResponses();
      for (var i = 0; i < formResponses.length; i++) {
       var formResponse = formResponses[i];
       var itemResponses = formResponse.getItemResponses();
        for (var j = 0; j < itemResponses.length; j++) {
          var itemResponse = itemResponses[j];
          if (itemResponse == futureDate) { // Unsure what this part is for, perhaps you should find another way to calculate this.
            count++; 
          }
        }
      }
    
      if (count != 2) {
        MailApp.sendEmail("[email protected]",
                          "Submit Form Reminder",
                          "Hey! You didn't fill out the form yet!");
      };
    }
    

    I was able to clean up your code to a way it should work, but not sure about futureDate in the reminderEmail function.