Help

Re: Setting multiple field values when creating records in a child table

814 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Tom_H
6 - Interface Innovator
6 - Interface Innovator

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

 

2 Replies 2
Sho
11 - Venus
11 - Venus

I'm not familiar with asynchronous processing.
How about debugging it once out of the function since you don't know where the error is occurring.
(Maybe if you write the exception handling properly, it won't be a problem.)

The definition of the field seems wrong to me.

 

let parenttbl = base.getTable('ListInfo');
let childtbl = base.getTable('List');
let {parentRecord,newRecordCount} = input.config();
let linkField = childtbl.getField('ListName');


// 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.name]: checked,
            [x2Field.name]: checked,
            [link2Field.id]: [{ id: link2Fieldid }],
            [quoteField.name]: 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);
}

 

 

Tom_H
6 - Interface Innovator
6 - Interface Innovator

Hi @Sho,

Thanks for your response!  Unfortunately my coding experience is fairly limited (I can understand the logic but syntax gets me every time).  I've tried adding breakpoints using 'debugger;' but code continues to error out at the same place.  

Thanks!

Tom