Help

Linking records from database ID's

Topic Labels: Scripting extentions
2661 3
cancel
Showing results for 
Search instead for 
Did you mean: 
jheddings
4 - Data Explorer
4 - Data Explorer

I’m attempting to migrate to Airtable from several custom SQL databases… The existing tables have several foreign keys that I am attempting to recreate in Airtable, but I cannot figure out how to update a record properly.

In the example below, I’m trying to re-link the Cities to the references from States. There is an existing DBID column holding the original database foreign key. Finding the reference State is easy enough (though inefficient at this point), but I cannot figure out how to update the record properly at the end of the loop. Any ideas?

let active_table = base.getTable("Cities");
let ref_table = base.getTable("States");

let query = await active_table.selectRecordsAsync();

for (let record of query.records) {
    let ref_id = record.getCellValue("StateRef");
    output.markdown(record.getCellValue("Name"));

    let ref_query = await ref_table.selectRecordsAsync();
    let ref_record = undefined;
    for (let find_ref of ref_query.records) {
        if (find_ref.getCellValue("DBID") == ref_id) {
            ref_record = find_ref;
            output.markdown("-> " + find_ref.getCellValue("Name"));
            break;
        }
    }
    
    active_table.updateRecordAsync(record, {
        State: ref_record
    });
}
3 Replies 3

Hi @jheddings,

I think you have to pass an array of records to a Linked record field in an updateRecordAsync() call, even if you are only adding a single record link.

image

active_table.updateRecordsAsync(record, {
    "State": [{id: ref_record.id}] 
});

That did it!! Thank you for the quick reply. It seemed easy enough, but I was missing the array declaration. I’m off and running again…

Yep, and you might also find this discussion relevant.