When you request the id of a trigger object using its getUniqueId()
method, the value returned is a numerical String.
However when a function is called by the trigger, the triggerUid
property of the event object will be that string converted to a Number.
The issue is that the string id often (but not always) expresses an integer value larger than the maximum integer value Apps Script can handle safely. When this happens the conversion from string to number results in the zeros in the least significant digits.
So, to compare the two properly you just need to convert the string id to a number, this ensures the same zeroing will occur.
someTrigger.getUniqueId() === event.triggerUid
// false - because the left side is a string, and the right is a number.
Number(someTrigger.getUniqueId()) === event.triggerUid
// true - both sides are now numbers, the same number
someTrigger.getUniqueId() == event.triggerUid
// true - because using the standard comparison operator the string is automatically converted to a number in the background
Here's a note about strict v.s. standard comparison operations in Javascript.
Here's some Apps Script that demonstrates all of the above:
// Run onceOffClockTimer() and wait up to 1 min for the event to fire, then check your log. You might have to run it a few times to see a trigger id where the zeroing occurred
function onceOffClockTimer() {
var newTriggerId = ScriptApp.newTrigger('timerCallbackFn')
.timeBased()
.after(5000)
.create()
.getUniqueId();
}
function timerCallbackFn(triggerEvent) {
var eventTriggerId = triggerEvent.triggerUid,
triggers = ScriptApp.getProjectTriggers();
Logger
.log('### Event Object ###')
.log('id: %s', eventTriggerId)
.log('id type: %s', typeof eventTriggerId)
.log('id as String: %s\n', eventTriggerId.toString());
var largestSafeInteger = 9007199254740991; // Obtained by Number.MAX_SAFE_INTEGER in a Chrome console
Logger
.log('### Interesting Fact ###')
.log('In Apps Script the largest safe integer is: %s\n', largestSafeInteger);
for (var i = 0, x = triggers.length; i < x; i++) {
var triggerID = triggers[i].getUniqueId();
Logger
.log('### Trigger Object [%s] ###', i)
.log('id: %s', triggerID)
.log('id type: %s', typeof triggerID)
.log('id as Number: %s\n', Number(triggerID))
Logger
.log('### Comparisons ###')
.log('Strict (Trigger Event ID === Trigger ID) is %s ', eventTriggerId === triggerID)
.log('Strict (Trigger Event ID === Trigger ID as Number) is %s', eventTriggerId === Number(triggerID))
.log('Direct (Trigger Event ID == Trigger ID) is %s', eventTriggerId == triggerID);
}
}