Skip to main content
Solved

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


Forum|alt.badge.img+10

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.

Best answer by Kasra

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.

View original
Did this topic help you find an answer to your question?

7 replies

Adnan_Ahmed
Forum|alt.badge.img+16
  • Inspiring
  • 59 replies
  • June 22, 2020

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


Forum|alt.badge.img+10
  • Author
  • Inspiring
  • 22 replies
  • June 22, 2020
Adnan_Ahmed wrote:

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


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?


Justin_Barrett
Forum|alt.badge.img+20
Josh_Cooper wrote:

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?


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


Forum|alt.badge.img+4
  • Inspiring
  • 192 replies
  • June 22, 2020

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


Forum|alt.badge.img+10
  • Author
  • Inspiring
  • 22 replies
  • June 22, 2020
Kasra wrote:

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!


Forum|alt.badge.img+4
  • Inspiring
  • 192 replies
  • Answer
  • June 22, 2020

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.


Adnan_Ahmed
Forum|alt.badge.img+16
  • Inspiring
  • 59 replies
  • June 25, 2020
Kasra wrote:

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?