Button & Scripting Block - Date/Time Field

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.

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!

2 Likes