Hi Everyone,
Looking for some help with improving an automation that I've used to create a variable amount of records in a child table and link it to the parent table based on inputs in the parent table. Here is the code that has worked.
let parenttbl = base.getTable('ListInfo');
let childtbl = base.getTable('List');
let {parentRecord,newRecordCount} = input.config();
let linkField = childtbl.getField('ListName');
async function createChildrenLinkedRecords() {
// Airtable limits batch operations to 50 records or fewer.
let maxRecordsPerCall = 50;
let newRecords = [];
// Part 1: Prepare the new records
for (let index = 0; index < newRecordCount; index += 1) {
newRecords.push({
fields: {
[linkField.id]: [{ id: parentRecord }],
},
});
}
// Part 2: Perform the record creation operations in batches
while (newRecords.length > 0) {
await childtbl.createRecordsAsync(newRecords.slice(0, maxRecordsPerCall));
newRecords = newRecords.slice(maxRecordsPerCall);
}
}
await createChildrenLinkedRecords();
What I'm trying to do is in the child table set the values for 4 additional fields, X1, X2, LinktoCount, and Quotation. Fields X1 and X2 are check boxes and I want to set their values to checked. LinktoCount is a linked field to a third table called ListCount and I want to link all of the newly created records in the child table to a single record in the List Count with it's primary key being "." and its record ID is recHphU0Q7GdrI6kN. Quotation is a text field and I want to set it's value to a single quotation mark ("). Here is my attempt at making this work but it will error out.
let parenttbl = base.getTable('ListInfo');
let childtbl = base.getTable('List');
let {parentRecord,newRecordCount} = input.config();
let linkField = childtbl.getField('ListName');
async function createChildrenLinkedRecords() {
// Airtable limits batch operations to 50 records or fewer.
let maxRecordsPerCall = 50;
const x1Field = childtbl.getField('X1');
const x2Field = childtbl.getField('X2');
const link2Field = childtbl.getField('LinktoCount');
const quoteField = childtbl.getField('Quotation');
const checked = true;
const link2Fieldid = 'recHphU0Q7GdrI6kN';
const quote = '"';
let newRecords = [];
// Part 1: Prepare the new records
for (let index = 0; index < newRecordCount; index += 1) {
newRecords.push({
fields: {
[linkField.id]: [{ id: parentRecord }],
[x1Field]: checked,
[x2Field]: checked,
[link2Field.id]: [{ id: link2Fieldid }],
[quoteField]: quote,
},
});
}
// Part 2: Perform the record creation operations in batches
while (newRecords.length > 0) {
await childtbl.createRecordsAsync(newRecords.slice(0, maxRecordsPerCall));
newRecords = newRecords.slice(maxRecordsPerCall);
}
}
await createChildrenLinkedRecords();
The error that I'm getting is as follows:
ERROR
Error: Could not find a field with name or ID "[object Object]".
at createChildrenLinkedRecords (script:41)
at main (script:47)
Any help or assistance that you can provide is greatly appreciated.
Thanks!
Tom