Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Jan 27, 2021 11:06 AM
Hi,
i have two tables.
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
Jan 29, 2021 08:52 AM
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.
Jan 30, 2021 05:38 AM
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
Jan 31, 2021 10:30 AM
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]
}
Jan 31, 2021 11:51 PM
Hi,
thanks a lot. That example helps me.
Have a great start in the week.
Kai