How to re-link previously linked records from an OLD base to a NEW base using airtable scripts

I’m working on an airtable script where I’m trying to re-link records that were previously linked in another base.
Context: I shared two tables from an OLD base to a NEW base. These tables had records that were linked(in the old base). After sharing them to the NEW base, the links got lost. I’m now trying to re-link these records. I have two tables, “Companies” and “People”. The “Companies” table has fields with the company name and employees. The “People” table has fields with people’s names and their affiliated company.

Here’s the script I have written to attempt to solve this issue.

//Define the company table and query
let cmpTbl = base.getTable("Companies");
let cmpQuery = await cmpTbl.selectRecordsAsync();

//Define people table and query
let pplTbl = base.getTable("People");
let pplQuery = await pplTbl.selectRecordsAsync();

for (let allPeople of pplQuery.records){
    let cmp = allPeople.getCellValue("Company")
    for (let allCompanies of cmpQuery.records){
        if (cmp === allCompanies.getCellValueAsString("Company")) {
            // console.log(cmp)
            // console.log(allCompanies.getCellValueAsString("Company"))
            let cmpID =
            await pplTbl.updateRecordAsync(allPeople, {"Company" : [{id: cmpID}]})

I essentially loop through the “People” table and extract the corresponding string value in the company field and compare that to the records in the “Companies” table after which I go ahead and link them. However, there’s an error:

any ideas on how to fix this issue? Seems like a fieldType issue but I have tried casting the ‘string’ ID to an and it hasn’t worked.

Your “Company” field is presumably a Single Line Text, right? If it were already a Link Field, then your records were already linked and there’d be no need for the script.

You can’t pass an array or object to a Text field. You have to pass a string.

If the name of the Company is the primary field of the Companies table, and your People table has a field where companies are comma separated, then just convert that field into a Link Records field. The old text field in Companies that lists People will be redundant, so you can delete it. Airtable will have created a new inverse-Link Field inside your Companies table upon field conversion.

1 Like

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