Aug 11, 2021 07:20 AM
Hi All
I’m building an app with Adalo using Airtable as my database.
I have, as of now, 1 base with two tables, users and products. A product can have many users and a user can have many products. Obviously there is a linked field connecting Users → Products and it’s a multi-select linked field (a product can have many users).
I’m trying to “add” a user or “append” a user to the user array through the airtable API (PATCH) and each time I get something to work, I can only get it to overwrite the current user in that table as opposed to adding a new user.
Any thoughts on what I’m missing here?
Thanks!
Aug 15, 2021 10:21 AM
Hi @Brian_McEuen - on the API docs you can see this for a linked field:
To link to new records in Link, add new linked record IDs to the existing array. Be sure to include all existing linked record IDs that you wish to retain. To unlink records, include the existing array of record IDs, excluding any that you wish to unlink.
So if you pass just the new linked record ID, you’ll be left with just that one. The way around this is to get the existing linked record IDs and add your new ID to this array. You can do this with the “spread” operator. The snippet below is from the internal Airtable scripting block, but the same approach would apply using the external API:
let newForeignRecordIdToLink = 'recXXXXXXXXXXXXXX';
myTable.updateRecordAsync(myRecord, {
'myLinkedRecordField': [
...myRecord.getCellValue('myLinkedRecordField'),
{ id: newForeignRecordIdToLink }
]
})
So, you have the existing array of linked records (myRecord.getCellValue('myLinkedRecordField')
) and the new linked field object ({ id: newForeignRecordIdToLink }
), using the spread operator (...
) to join these together.
More info here:
and nicely illustrated by this example:
let parts = ['shoulders', 'knees'];
let lyrics = ['head', ...parts, 'and', 'toes'];
// ["head", "shoulders", "knees", "and", "toes"]