Help

Create Multiple Linked Records

Topic Labels: Automations
Solved
Jump to Solution
1985 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Fernando_Valice
4 - Data Explorer
4 - Data Explorer

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!`);
}
1 Solution

Accepted Solutions
Kamille_Parks
16 - Uranus
16 - Uranus

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)
}

See Solution in Thread

3 Replies 3
Kamille_Parks
16 - Uranus
16 - Uranus

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)
}

This was super helpful. Thank you!