Benjamin Grandfond
Benjamin Grandfond

Reputation: 11

Wrong source and response in form triggers

I set a simple workflow with a Google Apps Script. I created the script project from a form A.

Here is the workflow:

  1. users submit a response for the form A
  2. the script creates a new response for the form B, submit it, and sends me the edition link by email
  3. I submit the response for the form B the script created for me
  4. the script sends me a recap of both form by email

To do so I have programmatically set up a trigger on the form B:

function addOnNewFormBSubmitTrigger() {
  ScriptApp.newTrigger('onNewFormBSubmit')
    .forForm(FORM_B_ID)
    .onFormSubmit()
    .create();
}

The function onNewFormBSubmit looks like that:

function onNewFormBSubmit(formSubmitEvent) {
  _sendAnalysisEmail(formSubmitEvent.source, formSubmitEvent.response);
}

The function is triggered but the the formSubmitEvent.source value is the form A and formSubmitEvent.response is the latest response for this form. I would expect the source to be the form B, as I attached the trigger on it.

EDIT:

Even passing a form instance instead of its ID does not work as I expect it

function addOnNewAlaysisSubmitTrigger() {
  var form = FormApp.openById(FORM_B_ID);
  ScriptApp.newTrigger('onNewFormBSubmit')
    .forForm(form)
    .onFormSubmit()
    .create();
}

Am I missing something?

Upvotes: 0

Views: 192

Answers (2)

Benjamin Grandfond
Benjamin Grandfond

Reputation: 11

Thank you @tehhowch, the link you gave me in the comments made me think that it may work if the script is not created from the Form. Thus I made a POC creating the script from a Spreadsheet and it works.

This is the script I made: Google Apps Script

And these are the logs generated by the two functions: function call logs from Stackdriver

As you can see I get two different responses submitted from the two forms. Now the problem is that the event is not a submit form event as defined in the documentation: https://developers.google.com/apps-script/guides/triggers/events#form-submit_4 but this is another story.

If you want to try, here are:

Upvotes: 1

Kleinast
Kleinast

Reputation: 53

Have you tried to use Form as parameter instead of just his Id.

Something like that:

var form = FormApp.openById('FORM_B_ID');
ScriptApp.newTrigger('onNewFormBSubmit')
    .forForm(form)
    .onFormSubmit()
    .create();

Upvotes: 0

Related Questions