Jan 19, 2022 08:54 AM
I need help to create a script for a button field such that when I click the button for any record, an adjacent (checkbox) field for that record gets ‘checked’.
Any help would be very appreciated.
Thank you in advance.
Jan 19, 2022 11:31 AM
Could you explain the use case in a bit more detail? Is there a reason why the checkbox field can’t be clicked directly? Is it prevented via user permissions?
Also, where will this button field be used? If it’s in an Interface, it’s not going to work. Buttons in interfaces can’t currently trigger scripts. An alternate approach that’ll work in that environment would be to use a single-select field that triggers an automation, which wouldn’t require a script.
Jan 19, 2022 12:52 PM
Thanks, Justin. Thanks for clarifying the limitation of the button in an interface. I didn’t realize that.
No matter, my question was still about the a button field int he Grid view. I admit the toggling a checkbox by clicking a button wasn’t the best example, but in general, I meant clicking a button field and being able to set a value of another field(s) for that record, whether it was a checkbox, formula, whatever.
So if I click a button for a record, I’d like the three fields listed below to have their values set as follows:
Basically, in clicking a button, the values of three other fields for that record get updated. I feel confident that I can tweak the script to what values these columns get updated to, but I don’t even know where to begin with writing such a script.
As I think about this more, I realize that I could accomplish this by using a checkbox as a trigger instead of a button. Set the checkbox as the trigger for an automation and when that checkbox is checked, update the values of field 1, field 2, and field 3 – but I’d like to do this with a button
Jan 19, 2022 01:38 PM
Can you clarify your level of coding experience, your level of interest in learning how to write Airtable scripts, and how much of a start you want to be given?
There are several sample scripts in the documentation. The documentation also has info on how to read and write field values for each field type.
This project is actually a fairly good one for learning how to write scripts for Airtable.
Note that you cannot set the value of a formula field.
Jan 20, 2022 11:23 AM
Hi @kuovonne ,
Thank you for sharing the documentation link. I’ll give that a read as well.
As for setting the value of a formula field, I meant setting it as the result of a formula. So for e.g. setting a formula to NOW() and formatted as a date to include time is what I meant.
Jan 20, 2022 01:21 PM
Thanks for the clarification on that point. It’s important to note, though, that the output of the
NOW() function isn’t precise. Per the documentation, that only refreshes roughly once every 15 minutes when a base is open, and about once per hour when it’s closed.
Thankfully a script doesn’t need to rely on a formula field to get the current time. That can be done directly in the script using the
Date() object. Get the current date and time—meaning the precise time that the line is executed—like this:
let datetime = new Date();
Then the script can insert that value into a date field as part of updating a record.
Jan 25, 2022 10:50 AM
Thank you so much. This is great!