I'm pretty new to google scripts. I'm having an issue with getting an intermittent "Exceeded maximum execution time" error several times a week, the trigger for this function happens every minute. My code is short, I use it to get a text notifications from an email where all shipping notifications get sent to. The emails that come through have been pre-processed through a server, so the text is very simple and is copied directly from the email to the text message.
Example text: "1 Package received on 2025/01/20 11:57AM"
My code usually takes about 1 second from start to finish, but several times a week it will exceed the 6-minute window. I have put in console.log to try and narrow it down to a specific section of code, but I just get an error in the execution log that says "Exceeded maximum execution time". I have attached a screenshot of a good execution and the exceeded time execution. Please note that the good execution didn't have any notifications, so you will see more console.log entries in my code below. Please let me know what I can do to stop this error from occurring.
Executions Log Screenshot
Here is my code:
function myFunction() {
//setup text number for sending notification
var textnum = "**********@vtext.com"
console.log("Script Started")
//send search criteria for processing emails
var threads = GmailApp.search("is:unread");
Utilities.sleep(250) //pause to allow all searched info to come back
console.log("Gmail searched")
//forward any new emails
if (threads.length > 0) {
console.log("Search threads > 0")
//process all emails that meet search criteria
for (var i = 0; i < threads.length; i++) {
console.log("Email " + i + " getting checked")
//find the body of the email
var message = threads[i].getMessages();
var body = message[0].getPlainBody();
console.log("Email " + i + " checked")
//forward email and mark as read
GmailApp.sendEmail(textnum , "", body);
console.log("Email sent")
threads[i].markRead();
console.log("Email marked as read")
//data log
console.log (body);
}
}
console.log("Script end")
}
This is an issue on the Google side, so there is nothing that you can do to prevent this error from happening. However there a couple of things that you can do:
Read and follow the directions given by Google on Developer product feedback
Here are a related issue that I found on January 21, 2025:
Keep monitoring the situation and take action as needed when necessary. Apparently, as that trigger is set to run every minute, you might not do something unless the trigger is disabled.