2
votes

We have an Apps Script app is triggered by the "on Form submit" event of a Google Form. We take the information that was collected in the form, and after making a copy of a predefined Google Document template, we write the form data to the newly created Google document. For the most part - this works, however there are days (like today) when numerous

"We're sorry, a server error occurred. Please wait a bit and try again."

errors are thrown. The line of code that it's thrown at is:

var copy = template.makeCopy(title);
Utilities.sleep(4000);
return copy;

I believe I have followed the best practice but using the Utilities.sleep() function, but it doesn't seem to help. Today alone I have seen this error come up at least 15 times! The frustrating part is that this is not consistent. As mentioned previously, the app can run for days with no error, then on other days I get a bunch of these errors.

I have seen an issue logged in Feb 2014 that I starred, but there doesn't seem to be any resolution to it.

Please can anybody point me in the right direction as to what I can do to stop seeing that error?

1
The times when the code does work, how long does it take to run? How big is this file that's being copied? If you copied the file manually, does it take a long time?Alan Wells
It normally takes between 2 & 3 minutes to run. The process is: 1) Get the correct template, 2) Make a copy of template. 3) Do a find and replace in the copy and fill new copy with form data 4) Add the person who filled out the form as an editor to the new copy. 95% of the time the application throws that error at step 2 :( So it's a straightforward DriveApp makeCopy() call, and as I mentioned before it works fine 80% of the time. I can't figure this one out :(gmac
If you are not making more than one copy per function run, and there isn't much data, then 2 or 3 minutes is a long time to run. You would need to evaluate how long each line of code is taking to run, and see where the problem is. I'm wondering if your replacement code is taking a long time.Alan Wells

1 Answers

1
votes

Looks like you are using DriveApp to make the copy. That's good. Instead of using:

return copy;

I'd get the Id of the new file, then open the file using the Id. And then get rid of the Utilities.sleep(). Put the ID into a global object:

Function One:

//declared OUTSIDE of any function, so it's global
var objProjectInfo = {};

function fncOne() {

  var copy = template.makeCopy(title);
  var theCopyId = copy.getId();

  objProjectInfo['copyId'] = theCopyId;
}

Function two:

function fncTwo() {
  //Open the new file by ID
  var theIdToUse = objProjectInfo['copyId'];
  var theNewCopy = DriveApp.getFileById(theIdToUse);

  //Write data to new copy
  .... code ....
};

This gets rid of the Utilities.sleep(); it explicitly opens the new file before being used, so there is no question of whether it exists or not at that point; and it avoids passing data with the return statement.