I want to create a directory in the drive if it doesn't already exist.
function CreateDirectory() {
var folderName="Example";
var Directory;
var fi = DriveApp.getFoldersByName(folderName);
if (fi.hasNext()) {
Directory = fi.next();
} else {
Directory = DriveApp.createFolder(folderName);
}
}
The function stops when the condition is reached:
Sorry, a server error has occurred. Please wait a bit and try again.
What is the problem and how can it be fixed?
There is nothing wrong with your code.
Google can not guarantee 100% service availability.
Explanation:
The error tells you that you need to wait a little bit before your execute that function again.
A potential workaround solution would be to use a try...catch statement and within the
catch
brackets include a code to automatically execute the function after some time.For example, you can create a time-driven trigger that executes
CreateDirectory()
after some time (e.g. 1 minute) if the function failed the first time.Solution:
In the following solution the logic is to manually execute the
toRun()
function. The latter will try to executeCreateDirectory()
. If an error occurs, it will create a time-driven trigger that will executeCreateDirectory()
after a minute (modify that to your needs). TheclearTrigger()
function is responsible for clearing all the previous triggers (if there are any) that are created because of that code.