You must remember that this code takes into account the time of the last modification of the file – which means that if you upload a file that has been created/last modified a long time ago, it won’t fire the automation.
Try to open the file, do a minor change and save it, so the last modified time is updated – and then upload it to the folder.
You can also edit this time range, modifying the time on the isRecentlyCreated() function.
function isRecentlyCreated(file) {
const now = new Date();
const threshold = 60 * 1000; // Check for files created in the last minute (adjust as needed)
return now - file.getLastUpdated() < threshold;
}
It seems that you are trying to execute the isRecentlyCreated() function manually there – that’s not necessary.
Go to the triggers section (the stopwatch icon, on the left bar) and check whether you have the triggers created or not. If you don’t, then go back to the code section and select the function main() and execute it. Then check back if the triggers are there.
With the triggers in place your project should start monitoring the folder for recently created files (and as I said before, you can adjust the threshold of how much time is “recently” for you case).
Hi @hugoassuncao thank you for this script, I know it will be so useful. I am close but not getting data to the webhook and think maybe I am missing a step.
Things I’ve done
entered folder ID
updated files in that folder
added webhook url
I can’t see a ‘Resources’ section in my Scripts interface but I did receive a pop up to link my Google account which I accepted
What isn’t working
‘Triggers’ is empty
I’ve run the script many times, and received one webhook response at one point (maybe the permission?) but it was empty
No specific errors are showing, but no data is getting to the webhook
I attach a couple of screenshots and would be grateful if you can see what I am doing wrong!
Did you executed the funciton main(), in order to enable the trigger that will make the script run?
See on your screencapture the “myFunction” dropwdown menu?
Select the “main” function and click “Run”.
Then go to the left side panel, on the “Triggers” section and check if there is a trigger created.
Google Apps Script needs to run this script on a time basis in order to send the new files payload to your webhook.
And keep in mind that the script consider the last update on the file – so if it’s an old file - not updated recently, won’t work.
It’s strange that you have a function named “myFunction” at this point.
I suspect you kind of encapsulated the code in that original function (that is created automatically, when you create a new project).
Do this: copy the original code again, erase everything you have and replace the folder ID and webhook URL again.
Then run the “main” function and go to the “Triggers” section to see if the trigger has been created. You should see a trigger to run the function “checkForNewFiles”.
@hugoassuncao thank you for your help.
I’ve found that the script has been working successfully with the webhook in Make, even though I still can’t see any triggers!
It also works whether or not it’s called myfunction or main - both are good.