Nov 14, 2021 09:51 AM
Hi - I am very new to scripting and looking for advice on how to update the existing script.
I grabbed this pre-made script from the Airtable library to create child records. In addition to creating the child records, I was hoping to update a field on that child record as well with an inputted value. Can anyone advise on what updates to make?
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: 'Parent table' }),
input.config.table('childTable', { label: 'Child table' }),
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);
while (!parentRecord) {
output.text('You must select a record.');
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();
Nov 15, 2021 11:56 AM
Hi,
here,
let newRecordCount = parseInt(await input.textAsync('Number of records to create'), 10);
add another input.textAsync line to add your text
don’t use “parseInt”, as you need text, not number
the name of variable (like “newRecordCount”) - choose yourself
here:
fields: {
[linkField.id]: [{ id: parentRecord.id }],
add your field.
(e.g.)
[linkField.id]: [{ id: parentRecord.id }],
"MyField": myVariable