Help

Re: Modifying the "Create child linked Records" Script to work in an Automation

3234 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Tom_H
6 - Interface Innovator
6 - Interface Innovator

Hello all,

I found the “Create child linked records” script and it does exactly what I need to do, but I want to trigger it from within an automation, and instead of entering the number of child records, pull that information in from the input.config() from the trigger of the automation.

I have a parent table called “ListInfo” and a child table called “List”. The primary key of the of the parent table is a field called “ListName”. The child table has a linked field also called “ListName”.

I would like this automation to create the “ListSize” number of child records in the child table each time a record in the parent table is created and meets the triggering conditions of the automation (the “ListName” is not empty, the “ListSize” is not empty and the record was created today) .

My input variables for the automation are “parentRecord” with a value of the record ID of the triggering record in the parent table and “newRecordCount” with a value of “ListSizeNum” which is a formula field that gives a value for the “ListSize” field since this is a linked field.
Here’s my latest attempt at an iteration to get this to work, to no avail.

let parenttbl = base.getTable('ListInfo');
let childtbl = base.getTable('List');
let {parentRecord,newRecordCount} = input.config();
let linkField = parenttbl.getField('ListName'):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.id }],
            },
        });
    }

    // 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();

Any pointers on what can be modified to get this to work are greatly appreciated.

Thanks!

Tom_H

5 Replies 5

What exactly is wrong with your edited version? Are you getting an error?

A quick glance and the only thing I can think of would be to change:

let linkField = parenttbl.getField('ListName'):childtbl.getField('ListName')

to just

let linkField = childtbl.getField('ListName')

Hi Kamille_Parks,

Thanks for the quick response! When I tried that in one of my iterations I was receiving the error:

ERROR
Error: Field "fldBZNjwVdR8Bd9uF" cannot accept the provided value.
    at createChildrenLinkedRecords on line 27
    at main on line 33

I was able to determine that the field “fldBZNjwVdR8Bd9uF” is referring to the linked record field “ListName” in the child table “List”.

Any additional thoughts or insights is greatly appreciated!

Thanks!

Tom

If parentRecord is coming from input.config(), does it even have a property of .id or is already the ID of the parent record?

I suspect you should be feeding that field [linkField.id]: [{ id: parentRecord }]

Hi @Kamille_Parks,

That was exactly what needed to be modified.

Thank you for such awesome assistance! If any one is looking here’s the final script that worked as intended:

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();

Thank you again!

Tom

Tom_H
6 - Interface Innovator
6 - Interface Innovator

Hi @Kamille_Parks ,

So as I’ve implemented this code my use case has been updated to not only needing to create child records but to set the values of 3 additional fields in the child table as well. The 3 additional fields are a linked field to another table (I’m using the “ListCount” table to enumerate my records in the child table based off of 2 fields), I’m looking for the static value of this field (“LinktoCount”) to be record id of the sole record in the ListCount Table, value is “.” and record id is “recN03cTL3dQ7VaQe”. The remaining two fields (“X1” and “X2”) are checkboxes that I want the value to be set to checked or true.

Initially I modified the code to just create the child records and link them to the parent table and set the value of the “LinktoCount” field to the record id in the “ListCount” table. This would produce the child records and have the two linked field correct but two major issues would occur in that if the newRecordCount was over 50 it would only produce 50 records and even if it was under 50 the code would run and then I would get the following error:
ERROR Error: network timeout at: https://api.airtable.com/v2/bases/appNGrvC4HQ1jzkWH/tables/tblL1wZ9M81c6dead/records/create at createChildrenLinkedRecords on line 29 at main on line 35

Now I’ve realized that I also want to set the “X1” and “X2” checkbox fields to checked or true. My current iteration of the code is as follows:

let parenttbl = base.getTable('ListInfo');
let childtbl = base.getTable('List');
let {parentRecord,newRecordCount} = input.config();
let linkField1 = childtbl.getField('ListName');
let linkField2 = childtbl.getField('LinktoCount');
let linkField3 = childtbl.getField('X1');
let linkField4 = childtbl.getField('X2');


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: {
                [linkField1.id]: [{ id: parentRecord }],
                [linkField2.id] : [{id: 'recN03cTL3dQ7VaQe'}],
                [linkField3] : ['checked'],
                [linkField4] : ['checked'],
            },
        });
    }

    // 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();

Several issues that I’m trying to rectify:

  1. The proper syntax to set a checkbox field to checked (the code in the form above fails: ERROR
    Error: Field “fldRGy04pUJZrYUsw” cannot accept the provided value.
    at createChildrenLinkedRecords on line 33
    at main on line 39)
  2. Once that is inputed properly for both the “X1” and “X2” fields I am anticipating that I will continue to have the network timeout error and it capping created records at 50.

Any thoughts or guidance on this are greatly appreciated.

Thanks!

Tom