Both of these methods will create a single trigger and replace a single trigger. If you need to create and replace multiple triggers, the code would be slightly different.
Method 1
Creates a new trigger, and replaces any existing triggers that are using the same event handler ("myFunction" in your example). This assumes that you won't change the name of your handler function between executions and that you don't have multiple triggers running with the same handler. It is the weaker of the two methods for those reasons. However, it is slightly easier to understand since it isn't using any services that aren't already in your code sample.
// this is the function that gets called by your trigger
function myFunction() {
Logger.log("I am an event handler");
}
function replaceTrigger(handlerName) {
const currentTriggers = ScriptApp.getProjectTriggers(); // get the projects triggers
const existingTrigger = currentTriggers.filter(trigger => trigger.getHandlerFunction() === handlerName)[0]
if (existingTrigger) ScriptApp.deleteTrigger(existingTrigger) // delete the existing trigger that uses the same event handler
// create a new trigger
if (existingTrigger[0])
ScriptApp.newTrigger(handlerName)
.timeBased()
.atHour(9)
.nearMinute(1)
.everyDays(1)
.create();
}
replaceTrigger('myFunction')
Method 2
Store a trigger ID when you create a trigger, then use that ID to delete it. The benefit of using this method is that it will allow you to change the name of your handler function, and it won't have side effects on other triggers in your project. It uses the PropertiesService, which you may be unfamiliar with.
function myFunction() {
Logger.log("I am an event handler");
}
function replaceTriggerWithId(handlerName) {
// read the existing trigger id from script properties and delete it if it exists
const currentTriggers = ScriptApp.getProjectTriggers(); // get the projects triggers
const existingTriggerId = PropertiesService.getScriptProperties().getProperty('triggerId');
if (existingTriggerId) {
const existingTrigger = ScriptApp.getProjectTriggers().filter(trigger => trigger.getUniqueId() === existingTriggerId)[0];
if (existingTrigger) ScriptApp.deleteTrigger(existingTrigger)
}
// create a new trigger and store its id in script properties
const triggerId =
ScriptApp.newTrigger(handlerName)
.timeBased()
.atHour(9)
.nearMinute(1)
.everyDays(1)
.create()
.getUniqueId()
PropertiesService.getScriptProperties().setProperty('triggerId', triggerId);
}
replaceTriggerWithId('myFunction');
You could set replaceTrigger
or replaceTriggerWithId
to run on a time trigger. When they are triggered they will delete any triggers that were defined with the same handler name (method 1), or whichever trigger was last defined by the trigger handler (method 2). After deleting the old trigger, they will create a new one.
References:
ScriptApp
PropertiesService