Use button and script beta to update the record with the button

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.

1 Like

Yes, I also think it would be very much appreciated if someone could help out :slight_smile: . 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

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?

1 Like

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

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())

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!

1 Like

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.

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?

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.