Create Multiple Linked Records

Hi,

I need to iterate and create multiple linked records in table2 from table1 (without selecting them). But when I run my code I get the following error: “Can’t create records: invalid cell value for field ‘Property’.
Cell value has invalid format: .0.0 must be an object.
Linked records field value must be an array of objects with property ‘id’ corresponding to linked record id.”

I assume I have to put the linked record id in the createRecordAsync function, but I don’t know how the syntax for that should be.

Here is the full code. Thank you!

let importTable = base.getTable('Master List');
let importView = importTable.getView('Active View');
let importQuery = await importView.selectRecordsAsync();
let secondTable = base.getTable('Table2');

for (let record of importQuery.records) {
    let fullName = record.getCellValue('Name');
    await peopleTable.createRecordAsync({
        "Person": fullName
    })
    output.text(`New record for ${fullName} created!`);
}

The syntax requires an array of objects, each object would have a single key-value pair of the record ID. With your current code that would be:

    await peopleTable.createRecordAsync({
        "Person": [{id: record.id}]
    })

You probably want to create multiple records at once for efficiency. That would mean replacing the for loop with something like:

let updates = importQuery.recordIds.map(id => {
   return {
      fields: {
         "Person": [{id: id}]
      }
   }
})

while (updates.length > 0) {
    await secondTable.createRecordsAsync(updates.slice(0, 50))
    updates = updates.slice(50)
}
1 Like

This was super helpful. Thank you!

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.