Help

Button & Scripting Block - Date/Time Field

Topic Labels: Scripting extentions
2527 12
cancel
Showing results for 
Search instead for 
Did you mean: 
McKenna_Johnson
5 - Automation Enthusiast
5 - Automation Enthusiast

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.

12 Replies 12

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!

Terrific Script, very helpful and beyond my ability to write. But I can use it, works like a charm! Thanks.

Olu_Adebajo
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi @Jeremy_Oglesby , I tried your script for an automation but I’m getting an error. I don’t know what I’m missing
Screen Shot 2022-03-01 at 12.36.28 PM

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?

CleanShot 2022-03-04 at 12.36.54@2x

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!

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.

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.

@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:

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.

Yes she is an employee, and provided support to my team offline.

Is it possible to tweak this a hair and have it add seven days to todays date and set the new date to that?

@Michael_Ali1 You could just replace the last three lines of the original script with the following four:

let record = await input.recordAsync('Record to timestamp', config.tableToUse)
let newDateTime = new Date();
newDateTime.setDate(newDateTime.getDate() + 7);
await config.tableToUse.updateRecordAsync(record, { [config.timeStampField.name]: newDateTime })