Batch Create using while loop with incrementing field value

Hi Experts,

I am trying to create multiple child records from a parent record and came arcross an existing script in the forums and updated it to suit my needs. My aim is to ask user for the parent table, child table, link field and for each link field add the user inputted no of entries into the child table. Where I am stuck is that for each entry I want to add and increment the Date as entered by user. What I am finding is that in the below code when the recor actually gets created the date for all records is created as the latest value of startdate instead of incrementally changing for each record of the linked field:

/**

  • Copyright 2020 Bocoup
  • Permission is hereby granted, free of charge, to any person obtaining a copy
  • of this software and associated documentation files (the “Software”), to
  • deal in the Software without restriction, including without limitation the
  • rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  • sell copies of the Software, and to permit persons to whom the Software is
  • furnished to do so, subject to the following conditions:
  • The above copyright notice and this permission notice shall be included in
  • all copies or substantial portions of the Software.
  • THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  • IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  • FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  • AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  • LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  • FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  • IN THE SOFTWARE.
    */

/**

  • Create many links script
  • Given a record in a “parent” table, create some number of “child” records in
  • another table, where each “child” references the “parent” through a Linked
  • Record field.
  • Notes on adapting this script.
  • The script prompts for input every time it is run. For some users, one or
  • more of these values may be the same with every execution. To streamline
  • their workflow, these users may modify this script by defining the constant
  • values in the first few lines. The values should be expressed as JavaScript
  • strings in the object named hardCoded.
    */
    ‘use strict’;

/**

  • Users may provide values for any of the properties in the following object
  • to streamline the script’s startup.
    */
    const hardCoded = {
    parentTableName: ‘’,
    parentTableViewName: ‘’,
    childTableName: ‘’,
    linkFieldName: ‘’,
    newRecordCount: ‘’
    };

/**

  • Do not edit any code following this message.
    */

// Airtable limits batch operations to 50 records or fewer.
const maxRecordsPerCall = 50;

const parentTable = hardCoded.parentTableName
? base.getTable(hardCoded.parentTableName)
: await input.tableAsync(‘Parent table name (holds the existing record)’);
const parentTableView = hardCoded.parentTableViewName
? parentTable.getView(hardCoded.parentTableViewName)
: await input.viewAsync(‘Parent table View name (holds the view of existing record)’, parentTable);
// let parentRecord = recordAsync(‘Parent record’, parentTableView);
let queryResult = await parentTableView.selectRecordsAsync();
console.log (Total of ${queryResult.records.length} records found);
const childTable = hardCoded.childTableName
? base.getTable(hardCoded.childTableName)
: await input.tableAsync(‘Child table name (holds the new records)’);
const linkField = hardCoded.linkFieldName
? childTable.getField(hardCoded.linkFieldName)
: await input.fieldAsync(‘Link field’, childTable);
const linkDateField = hardCoded.linkFieldName
? childTable.getField(hardCoded.linkFieldName)
: await input.fieldAsync(‘Date field’, childTable);
let startfromdate = await input.textAsync(‘Start Date for records (format DD-MM-YYYY)’);
let startdate = new Date(startfromdate);
console.log (${startdate});
const newRecordCount = hardCoded.newRecordCount
? parseInt(hardCoded.newRecordCount, 10)
: parseInt(await input.textAsync(‘Number of records to create’), 10);
let newRecords = ;

// Part 1: Prepare the new records
for (let record of queryResult.records) {
for (let index = 0; index < newRecordCount; index += 1) {
console.log (updated Start Date is ${startdate});
newRecords.push({
fields: {
[linkField.id]: [{id: record.id}],
“Date”: startdate
}
});
// await childTable.createRecordsAsync([
// {
// fields: {
// [linkField.id]: [{id: record.id}],
// “Date”: startdate,
// },
// },
// ]);
startdate.setDate(startdate.getDate() + 1);
console.log (Incremented Start Date is ${startdate});
}
startdate.setDate(startdate.getDate() - newRecordCount);
console.log (Old Start Date is ${startdate});
}

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

It looks like you are pushing the same date object to all of the records, so they all have the same value. Try converting the date to an ISO string when you push the field values so that each record has a different string value.

This topic was solved and automatically closed 15 days after the last reply. New replies are no longer allowed.