Jun 20, 2020 02:02 PM
I would like to write a simple script block triggered by the new beta button to update fields in the record which the button was push. The use can is this. Our team uses a base to track orders through a production environment: planning to operations to lab to shipping. There are several fields that have to be updated to move the records from one department’s view to the next and it would be great to automate these updates with a button.
I have no javascript experience so I really don’t know where to even start. Any help would be greatly appreciated.
Solved! Go to Solution.
Jun 22, 2020 12:51 PM
Glad you got it working! There isn’t a way to prevent the scripting block from opening at the moment. It’s often useful to show some indication to the person who clicked the button whether the script ran successfully or not.
Jun 22, 2020 08:57 AM
Yes, I also think it would be very much appreciated if someone could help out :slightly_smiling_face: . I also have no knowledge about this, but with all the cool things everyone’s testing out I feel I’m missing out on a lot of cool stuff and tricks that could help work around a lot of existing limitations
Jun 22, 2020 09:14 AM
I was able to piece together the code to update a single-select field.
let table = base.getTable("Orders");
let field = table.getField('Order Info');
let record = await input.recordAsync('Choose a record', table);
let cellValue = record.getCellValue(field);
let currentId = record.id
output.markdown(`# You have updated ${cellValue}.`);
let changeField = table.getField("testDropDown");
let originalValue = record.getCellValue(changeField);
let newValue = "Yes";
table.updateRecordAsync(record,{
"testDropDown":{name:newValue},
})
Now, I need help to do the same thing with a Date field. I have tried using the same method above with the Date.now() function, but this returns a number and evidently the Date field type in Airtable is stored as a string.
Anyone have any ideas?
Jun 22, 2020 09:32 AM
The “Cell values & field options” section of the Scripting block API (below the code editor) has detail on all fields and their respective read/write types. For date fields, it says:
When reading from and writing to a date field, the cell value will always be an ISO 8601 formatted date.
The date format string follows the moment.js structure documented here
Jun 22, 2020 11:48 AM
Specifically for dates, you can pass in a Date
object (e.g. new Date()
) or a ISO-formatted string, which can get using the Date.toJSON
method (e.g. new Date().toJSON()
)
Jun 22, 2020 12:10 PM
Thanks for the help everyone!.
I was able to change the new Date() number to a string and pass it back to a date field using the toISOString and slice methods.
var rightNow = new Date();
var res = rightNow.toISOString().slice(0,10);
table.updateRecordAsync(record,{
"testDate": res
})
This seems to work well.
Any idea if this will work without the scripting block actually opening up? Or, in other words, can you prevent the scripting block from opening when you push the button?
Thanks again for all the help!
Jun 22, 2020 12:51 PM
Glad you got it working! There isn’t a way to prevent the scripting block from opening at the moment. It’s often useful to show some indication to the person who clicked the button whether the script ran successfully or not.
Jun 24, 2020 05:54 PM
Hello @Kasra, are there any plans to make the button field to work similar to the ‘batch’ update block? In that I can set actions from a similar UI without writing scripts, and without having the script block open up?