Help

Welcome to the Airtable Community! If you're new here, check out our Getting Started area to get the most out of your community experience.

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

Topic Labels: Scripting extentions
Solved
Jump to Solution
191 1
cancel
Showing results for 
Search instead for 
Did you mean: 

Hi!
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();
console.log(cmpQuery)

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


for (let allPeople of pplQuery.records){
    let cmp = allPeople.getCellValue("Company")
    console.log(cmp)
    for (let allCompanies of cmpQuery.records){
        if (cmp === allCompanies.getCellValueAsString("Company")) {
            // console.log(cmp)
            // console.log(allCompanies.getCellValueAsString("Company"))
            let cmpID = allCompanies.id
            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:
Screen Shot 2022-07-13 at 11.15.25 AM

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

1 Solution

Accepted Solutions

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.

See Solution in Thread

1 Reply 1

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.