This might be a weird one but I have some code that goes through my email and uses labels to trigger certain tasks. For example, if someone applies via Indeed, Indeed sends me an email. A filter will see this this email and apply the "Applications/indeedApplication" label. Then, a script that runs once per minute will find this label, and based on the label, perform some actions automatically.
This works perfectly about 99.97% of the time. The script runs with no issues and does the tasks, applies a completed label, and everything works as intended. 0.03% of the time, the task will fail, always at the same point, and always with the same exception:
Exception: Gmail operation not allowed.
It fails when the scrip tries to run the getLables() function on a GmmailThread (class of GmailApp API).
Code in question:
function addICandidate() {
var label = GmailApp.getUserLabelByName("Applications/indeedApplication"); //Defines the label that identifies indeed applicatoins
var requests = label.getThreads(); //Returns an array of threads which have this label
if(requests.length > 3){requests.length = 3}; //This makes it so the script doesn't waste time looking through every application ever.
for ( var i in requests ) { //Starting a normal for loop
iLabels = requests[i].getLabels(); //Gets the labels of the Randomly fails here and only here
var checker = false; //used later to check if tasks were already completed
for (var k = 0 in iLabels) { //Starting another normal for loop
var individualLabel = iLabels[k].getName(); //Gets a list of the labels
if (individualLabel === completedLabelName) { //Checks if the label for completes tasks is present
var checker = true; //If they are, set checker to true (also used later)
}
}
if(checker){ //If the checker above is set to true
Logger.log("true"); //Do nothing (logger is just used in debugging)
}
else{ //Else (if the label is not completed and therefore, the tasks have not been done yet)
var thread = requests[i].Dostuff(); //Confidential stuff lives here
}
requests[i].addLabel(completedlabel); //After everything, apply the completed label so tasks are not done again on the next pass
}
}
}
Does anyone know why it would fail here and only 1 in 3000 attempts. I obviously cannot debug this with brute force so any ideas as to why this might happen would be really helpful.