Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Re: Field ID instead of Field Name?

4955 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Bill_Carovano
6 - Interface Innovator
6 - Interface Innovator

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

7 Replies 7

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.

Kasra_Kyanzade1
6 - Interface Innovator
6 - Interface Innovator

You can see the underlying field IDs using this script:

for (const table of base.tables) {
    output.text(table.name);
    output.table(table.fields)
}

Screen Shot 2020-02-29 at 8.03.10 PM

You can substitute the IDs for the names in the API, e.g. record.getCellValue(fieldId)

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)
    

}

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.

shanemileham
4 - Data Explorer
4 - Data Explorer

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);
Nick_Truch
5 - Automation Enthusiast
5 - Automation Enthusiast

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?

Slightly better:

for (const table of base.tables) {
    output.markdown(`# ${table.name} (${table.id})`);
    output.table(table.fields)
}