Aug 20, 2020 09:42 AM
I am looking for a script to insert the current date and time into a field when you hit a button.
Currently, my users just hit the “Today” button when the calendar pops up in the date-time field when they begin a task but I would like to just have a “Start Task” button.
I am new to scripting and would appreciate it if someone could walk me through this first application.
Aug 20, 2020 10:40 AM
Hi, @McKenna_Johnson! It sounds like you already have your target ‘Date’ field set up.
So first, sign up your workspace for the “Script settings beta”. This will make configuring your script much easier. The sign up is very simple, and there’s no waiting… you’ll just need to refresh your workspace to activate it.
Next create the script block, and paste in this script:
const config = input.config({
title: "Timestamp with Button",
description: "Use the associated Button field to fill Timestamp field with current DateTime.",
items: [
input.config.table('tableToUse', {
label: 'Table to Use'
}),
input.config.field('timeStampField', {
label: 'Field to fill with current DateTIme',
parentTable: 'tableToUse'
})
]
})
let record = await input.recordAsync('Record to timestamp', config.tableToUse)
let currentDateTime = new Date()
await config.tableToUse.updateRecordAsync(record, { [config.timeStampField.name]: currentDateTime })
After you click “Finish Editing”, the new “Script settings” will allow you to select your Table and target Date field (the one you want to fill with the timestamp) as persistent options for every time the script runs, and you won’t have to worry about your script breaking if you change the names of those in the future.
Finally, create your Button field and point it at this script. The script will automatically pick up the Record the button was clicked from and fill in your ‘Date’ field with a current timestamp.
Ping back if you run into any issues!
Jun 16, 2021 07:55 AM
Terrific Script, very helpful and beyond my ability to write. But I can use it, works like a charm! Thanks.
Mar 01, 2022 09:43 AM
Hi @Jeremy_Oglesby , I tried your script for an automation but I’m getting an error. I don’t know what I’m missing
Mar 04, 2022 11:40 AM
I’m not sure what the issue is there, either, @Olu_Adebajo. I just tried my script again in a new base and it works fine. Did you select a table to use in the script settings options?
I feel like the error you are getting is indicating that script settings (the API that uses the input.config
object) is not active in your account. When I wrote this post in 2020, that feature was still in beta. I can’t imagine that is still the case… So I’m confused why this wouldn’t be working for you!
Mar 04, 2022 12:12 PM
The problem is that the script is written to run from a button in Scripting App, but @Olu_Adebajo is trying to run the script from an automation. input.config()
works very differently in the two environments.
Writing a script to put a time stamp on a record is fairly simple once you know how to write an automation script. If you are interested in learning scripting, this is a great beginner project.
If you are not interested in learning scripting, one of my automation helpers is a script that outputs a timestamp that can be used in a future automation step. No coding knowledge needed. Just insert the script into a scripting action. Then add an “Update Record” action that updates the triggering record with the value output from the script.
Mar 04, 2022 12:22 PM
Ah, I see. I skimmed right over that bolded part of his post that indicated he’s using it in automations :crazy_face: . Thanks for pointing that out, @kuovonne.
Mar 08, 2022 09:46 AM
@kuovonne I picked up your automation helper, and it did exactly what you said—which was most of what I needed my automation to do.
@Jeremy_Oglesby yes, your script is sound, but unfortunately like @kuovonne mentioned, this was an automation.
@Courtney was actually able to help me finally figure out why it wasn’t working and still throwing the error!
Thanks everyone! The @Airtable community is strong :pray:t5:
Mar 08, 2022 10:02 AM
Thank you for posting the followup.
I don’t see a post from @Courtney in this thread. Is Courtney an Airtable employee? Did she provide a solution offline? It would be really cool if Airtable is being proactive about answering support questions here.
Mar 18, 2022 10:58 AM
Yes she is an employee, and provided support to my team offline.