Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Feb 29, 2020 01:32 PM
I’ve been testing out the Scripting block over the past few days. This is the most exciting thing to happen to Airtable…since Airtable :slightly_smiling_face:
Does anyone know of a way to make the scripts resilient to changes in a table’s field names?
For example, if I have the following code in a script:
fname = record.getCellValue(“First Name”);
this works great until someone decides to rename the “First Name” field to “Contact First Name” – at which point the script will no longer work.
I’m wondering if there is some sort of “internal ID” for a field that stays the same even if the visible field name is changed.
Thanks
Bill
Feb 29, 2020 02:55 PM
Not as far as I can tell. You can get the list of field names from the table, but I don’t see any other internal id. You could compare the field name in your code to the list of field names, and fail gracefully if it isn’t there. It would probably be easier to strictly limitCreator privileges and warn people to not change things.
Feb 29, 2020 08:04 PM
You can see the underlying field IDs using this script:
for (const table of base.tables) {
output.text(table.name);
output.table(table.fields)
}
You can substitute the IDs for the names in the API, e.g. record.getCellValue(fieldId)
Mar 01, 2020 06:39 AM
Super helpful! Thanks Kasra!
I figured that I would want to make my table references resilient to name changes as well, and found that this additional code provided me the table ids.
for (const table of base.tables) {
output.text(table.name);
output.text(table.id); // table ID
output.table(table.fields)
}
Mar 03, 2020 02:27 AM
I just realized that this underlying field id is what replaces the field name in a formula after the field is deleted. I had wondered where those strings came from. Of course, knowing the field id only after the field is deleted isn’t very useful.
I wonder if the field id from the Airtable Scripting API works in the Airtable Standard API.
Eta. Looks like it does according to this topic.
Mar 14, 2020 08:21 AM
If you want to initialize an object for use in your code later, you can get all the ids in an object like this:
let allTables = {};
for (const table of base.tables) {
let newObject = {}
newObject.id = table.id;
for (const field of table.fields) {
newObject[`${field.name}`] = field.id
}
allTables[`${table.name}`] = newObject;
}
output.inspect(allTables);
Feb 04, 2021 01:41 AM
I’ve come across the same issue where I need a unqiue ID for each record.
Just found the Autonumber field type. Is there a reason why you’re not using this?
Sep 17, 2021 09:50 AM
Slightly better:
for (const table of base.tables) {
output.markdown(`# ${table.name} (${table.id})`);
output.table(table.fields)
}