Help

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

Re: Issues with a script - Link to another record

1412 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Kai_Dickas
6 - Interface Innovator
6 - Interface Innovator

Hi,

i have two tables.

  1. Keywords (2 Views: 1st “All records”, 2nd “Records without topic”)
  2. Topics

After importing new keywords to the Keywords Table i 'm using a script to link all keywords without a topic to a topic which i define while i’m running the script.

The first time i’m using the script - f.e. i’ve just imported 3 Keywords (Keyword 1, Keyword 2, Keyword 3) and after that running the script to link them to “Topic 1” of the Topics Table - everything works as expected.
But if i import another set of keywords (Keyword 4, Keyword 5, Keyword 6) and run the script again - again i like to link all of the just imported keywords to “Topic 1” - the first three keywords of the first import gets unlinked from topic 1 and the new imported keywords gets linked to “Topic 1” instead.

Seems i’m making a mistake in the script but i have no clue what to do to make it work.
Could anybody help me out here? Thanks a lot.

Kai

This is the script i’m using

//Keywords einem Thema zuordnen//

let topicsTable = base.getTable(‘Topics’);
let topicsRecord = await input.recordAsync(“Wähle ein Thema, dass den Keywords zugeordnet werden soll”,topicsTable)
let keywordsTable = base.getTable(‘Keywords’).getView(‘Keywords without topic’);
let keywordsQuery = await keywordsTable.selectRecordsAsync();
let keywordsRecords = keywordsQuery.records;

let d =
keywordsRecords.forEach(c => d.push({id: c.id}));
await topicsTable.updateRecordAsync(topicsRecord.id,{

'Keywords': d

}

);
output.markdown(’# Erledigt :rocket: ’)

Here is the link to my test base: Sign up - Airtable

4 Replies 4

When you update the field, it overwrites whatever value existed before. The answer is to read the existing linked record values (if any) and append you new values to the list.

Hi,

thank you for the info.
I’m a newbie when it comes to write scripts.
Do you may have a sample script part for me that will do what you’ve described?

Have a wonderful weekend.

Kai

You get the existing value of the linked record field with getCellValue

let existingValues = topicsRecord.getCellValue("Keywords")

You can test if there are any existing values or not, and if there are use the spread operator ... to join the array of existing values with your current array.

if (existingValues) {
  d = [...existingValules, ...d]
}

Hi,

thanks a lot. That example helps me.
Have a great start in the week.

Kai