How to make an idempotent GAS?

70 views
Skip to first unread message

Vikrant Tyagi

unread,
Jul 27, 2021, 3:25:31 AM7/27/21
to Google Apps Script Community
Hi There,

I have an App Script that pulls out events from my calendar where I'm the host and sends a Slack notification to the participants requesting them to fill a "post-meeting feedback" form.

Ideally, I would love to do this via some sort of web-hooks so I know when an event ends, but unfortunately, there's not such webhook as far as I know. 

So I resorted to a time-based trigger, let's say every 4 hours. Problem is that every time the trigger runs it runs for all the even in that day. So the notification for events from the early morning gets sent more than once.

How can I make this action idempotent? So if the scrip has already run for an even before, it should not run again.

Best Regards
-Vikrant

Joshua Snyder

unread,
Jul 27, 2021, 8:11:49 AM7/27/21
to Google Apps Script Community
Making a script idempotent can always be solved by giving every action a unique ID and storing that ID somewhere.

Here's what I suggest: Have your script look at events that have finished in the last 4 hours, every time it sends a notification, store a unique id (made up of the event id and the participant email address concatenated together) in an array.

Save the array in the Script Cache with an expiry > 4 hours, and whenever you send a new notification, check that the id of the notification doesn't exist in the array.

Sohail

unread,
Jul 28, 2021, 9:48:22 AM7/28/21
to Google Apps Script Community
An alternative to the given approach may also be the following:

Since the event's end time is already defined (at least to my understanding), the script can have the logic to see and capture the end time within the last 4 hours and send only the notification to those.

Clark Lind

unread,
Jul 28, 2021, 12:56:25 PM7/28/21
to Google Apps Script Community
Another possible alternative (WARNING: I have no idea if this will actually work)  is copy (retrieve via Apps script) some of the event info to a sheet, such as eventId, Title, Date, StartTime and EndTime. 
Then you could say, add some conditional formatting to the EndTime column(something like:  if now() > endTime mark it red or something). 
Then you set up an onChange() trigger to fire your email. 
And finally, if you go to File --> Spreadsheet Settings, and select the  Calculation Tab, change the Recalculation setting to "On change and every minute". That will tell the spreadsheet to update the Now() value. Once the condition is met,  the Conditional format should change the cell color, which should fire the onChange() trigger, thus sending an email. 

I hope that all makes sense. I don't actually know if the above would work, it is a hypothesis. :)

cbmserv...@gmail.com

unread,
Jul 28, 2021, 2:14:47 PM7/28/21
to google-apps-sc...@googlegroups.com

That can work, but it seems a little more complicated to implement.

 

What I would do is simplify this to a once a day trigger which will run through all the events and fire off the followup emails that you require. I don’t know if your situation requires more timely followup emails, but timing it to precise timely triggers for each event seems a little complicated to achieve and maybe overkill (most ppl do not respond to emails that quickly anyway).

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/111da2a7-cf05-4308-ac04-7143c77fb75bn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages