I am new to Scripting and need assistance with modifying the following Airtable Script, which is working well with the Base that I am creating. Unfortunately, I do not want the script to request user input for the number records to create; instead, I want it to use the number that is contained with in a field in the GOODS IN table. Can anyone help please?
let settings = input.config({
title: 'Create child linked records',
description: `For a record in a "parent" table, this script will create some
number of “child” records in another table, where each “child” references the “parent”
through a Linked Record field.`,
items: [
input.config.table('parentTable', { label: 'GOODS IN' }),
input.config.table('childTable', { label: 'LOTS IN' }),
input.config.field('linkField', {
parentTable: 'childTable',
label: 'Linked record field',
}),
],
});
async function createChildrenLinkedRecords() {
let { parentTable, childTable, linkField } = settings;
if (
linkField.type !== 'multipleRecordLinks' ||
linkField.options.linkedTableId !== parentTable.id
) {
output.text('Linked record field must be of type linked record to parent table.');
return;
}
// Airtable limits batch operations to 50 records or fewer.
let maxRecordsPerCall = 50;
let parentRecord = await input.recordAsync('Parent record', parentTable);
let newRecordCount = parseInt(await input.textAsync('Number of records to create'), 10);
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 childTable.createRecordsAsync(newRecords.slice(0, maxRecordsPerCall));
newRecords = newRecords.slice(maxRecordsPerCall);
}
output.text('Done');
}
await createChildrenLinkedRecords();