Link multiple records from array of records IDs

I am trying to, with scripting, link multiple records to one record. The code I have below succesfully links one record, and I’ve tried using a for loop, as well as a forEach to get it to cycle through the record Ids in the ‘matchTKRecords’ array. I’ve attached a picture of the console.log output for the matchingTKRecords array.

To clarify - my aim is to link the VMrecordID record to any/all of the records in matchingTKrecords

The error I get is always some variation on:
Error: Field "Linked tickets" cannot accept the provided value. at main on line 46

what I don’t understand is why an array of record IDs is not accepted as a write value for a linked tickets field (it is set-up correctly as a ‘link to another record’ field). Linking to multiple records is enabled

let newForeignRecordIdToLink = matchingTKRecords[0].id;
VMtable.updateRecordAsync(config.VMrecordID, {
'Linked tickets': [{ id: newForeignRecordIdToLink }]
   
})

consolelog

Thank you in advance for any assistance!

You’re passing an array of objects that includes the name property. The objects should only include the id property (which is why your example linking the first from that array works).

Without seeing the rest of the code, you will likely have to do something like this:

let newRecordIdsToLink = matchingTKRecords.map(record => ({id: record.id}));
VMtable.updateRecordAsync(config.VMrecordID, {
    'Linked tickets': newRecordIdsToLink
})
1 Like

Thanks for the response! I changed the code as follows:

let newForeignRecordIdToLink = matchingTKRecords.map(record => ({id: record.id}));

await VMtable.updateRecordAsync(config.VMrecordID, {

    'Linked tickets': [{ id: newForeignRecordIdToLink }]

   

})

However I get the error:

### ERROR

Error: Field "Linked tickets" cannot accept the provided value.

at main on line 61

EDIT: Realised my mistake - should be

let newForeignRecordIdToLink = matchingTKRecords.map(record => ({id: record.id}));

await VMtable.updateRecordAsync(config.VMrecordID, {

    'Linked tickets': newForeignRecordIdToLink

})

As you indicated! Thank you so much!