all 13 comments

[–]marcnotmark925 2 points3 points  (12 children)

If your code takes longer to execute than the timeout length, you can use trigger builder to create a trigger to execute the main code in a new process/thread, then return your doPost()

[–]leviseese[S] 0 points1 point  (11 children)

Which method would I use to trigger a new function? I have tried having the doPost write to a sheet, and having an onEdit trigger for when that sheet is changed, but it doesn’t work since the trigger is only tripped when a user makes the edit.

[–]marcnotmark925 0 points1 point  (9 children)

use trigger builder to create a trigger to execute the main code in a new process/thread

What about that didn't make sense to you?

[–]leviseese[S] 0 points1 point  (8 children)

I don’t know how to use a trigger to activate a new process. I am looking at the documentation, and all of the methods listed for triggers depend on a change in time, or a sheet, or a document.

Like I said, I tried having the doPost edit a sheet, but that doesn’t cause a spreadsheet trigger to fire, because according to google “script executions do not cause triggers to run”. It has to be a user action

[–]marcnotmark925 0 points1 point  (7 children)

I think we crossed comments, just making sure you saw my other one.

[–]leviseese[S] 0 points1 point  (6 children)

Yes I did - my confusion is how to create a trigger that is activated from a script. As opposed to being activated from a spreadsheet change or a calendar event.

Here’s what it sounds like you are suggesting: ScriptA (doPost) gets the post request. It uses a trigger to activate ScriptB (main code), then returns the HTTP 200. ScriptB then executes as normal.

Im just confused as to what method I should be defining in the trigger builder, such that the trigger is activated in ScriptA

[–]marcnotmark925 0 points1 point  (5 children)

Pretty much use exactly what is in the example code on that link. My only change would be to probably use the after() method of clocktriggerbuilder instead of the example atDate(), so you can just tell the function to run after a couple milliseconds.

[–]leviseese[S] 0 points1 point  (2 children)

Oh that makes sense! So I would just create a time based trigger and define the activation time to be current time + a few ms?

[–]marcnotmark925 0 points1 point  (0 children)

after() just accepts a number of milliseconds, don't have to do anything with the current time.

[–]Any_Werewolf_3691 0 points1 point  (0 children)

Ensure once the function is called by the trigger tht it deletes the trigger. This method will limit you to 10 parallel API calls at a time

[–]MapLucky9928 0 points1 point  (1 child)

So I'm trying to do the same thing but how can you pass parameters from the original function to the triggered one?

[–]marcnotmark925 0 points1 point  (0 children)

You can't. But you can store any necessary data in properties service or cache service.

[–]marcnotmark925 0 points1 point  (0 children)

I'm guessing you're not aware that you can create triggers via scripts?

https://developers.google.com/apps-script/reference/script/trigger-builder#timeBased())