I have this script in a Google form:
function onSubmit()
{
var response = FormApp.getActiveForm().getResponses()[FormApp.getActiveForm().getResponses().length - 1].getItemResponses()
if (response[2].getResponse()[0] === 'Notify the community manager of a livestream report in real time?' && response[1].getItem().getTitle() === 'Page/Group Identification')
{
UrlFetchApp.fetch('https://discord.com/api/webhooks/824374930422497331/X', {headers: {'Content-Type': 'application/json'}, method: 'POST', payload: JSON.stringify({content: '<https://www.facebook.com/' response[1].getResponse() '>'})})
}
if (response[2].getResponse()[0] === 'Notify the community manager of a livestream report in real time?' && response[1].getItem().getTitle() === 'Channel Identification')
{
UrlFetchApp.fetch('https://discord.com/api/webhooks/824374930422497331/X', {headers: {'Content-Type': 'application/json'}, method: 'POST', payload: JSON.stringify({content: '<https://www.youtube.com/channel/' response[1].getResponse() '>'})})
}
if (response[2].getResponse()[0] === 'Notify the community manager of a livestream report in real time?' && response[1].getItem().getTitle() === 'Account Username')
{
UrlFetchApp.fetch('https://discord.com/api/webhooks/824374930422497331/X', {headers: {'Content-Type': 'application/json'}, method: 'POST', payload: JSON.stringify({content: '<https://www.tiktok.com/@' response[1].getResponse() '>'})})
}
}
It works like a charm, but an issue has popped up after some months. After form submission, it takes 60-80 seconds before the webhook gets triggered. I think it is because the form spends a lot of time going through the 150,000 responses and finding the last one. When I created a copy of the form, the webhook was fired instantly upon form submission. Is there a way to optimize how the form reads the last response?
CodePudding user response:
Forms has a built in message to find the form that triggered an event. See below to establish a method to be guaranteed to get latest response. Alternatively, consider the second approach to just go with latest -1.
/**
* Executed when new entry received. This needs to be setup using EDIT ---> Current Project Triggers
*/
function entryMade(e) {
reviewResponse_(e.response);
}
This should also work with minimal overhead:
function getLatestResponse() {
var allResponses = FormApp.getActiveForm().getResponses();
var latestResponse= allResponses[allResponses.length - 1];
return latestResponse;
}