Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Re: Create multiple linked records with a button

Solved
Jump to Solution
6052 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Mathias_Elmose
7 - App Architect
7 - App Architect

Hi

I would like to be able to create multiple linked records in one “run”.

I do a lot of videos for lectures and have a base has with a “productions” table and a “projects” table.

I want to be able to

  • add a button in the “projects” table that runs a script
  • the script should ask me how many new records to create in the “productions” table.
  • and then create the records linked to the project

This would help me when 1 projects has 30 productions.

And because a higher demand for remote learning I get many request for many videoes - so time saved on place it time used in another:)

Thanks!

1 Solution

Accepted Solutions
Mike_Pennisi
7 - App Architect
7 - App Architect

@Mathias_Elmose Your script is so close, probably you’ve already finished it. Here’s a complete version, just in case you (or anyone following along out there) still need a hand:

Click to expand
/**
 * 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: '',
    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 parentRecord = await input.recordAsync('Parent record', parentTable);
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 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 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');

See Solution in Thread

11 Replies 11

Do the newly created [Productions] records need any information filled out other than the field linking it to the [Projects] table? If not, then you could use the following process:

  • Use an input to get the project (either from running the script normally or from a button click):
    const project = await input.recordAsync("Pick a project", projectTable)
  • Use another input to get the number of productions to add:
    const number = await input.textAsync("How many productions to add")
  • Use a loop to create the values for the new productions records:
    const values = []
    while (i < number) {values.push({fields: {"The name of the link field": {id: project.id}}})}
  • Create the new productions records:
    const newRecords = await productionsTable.createRecordsAsync(values)

Thanks Kamille!

I would just need to create the linkeds productions.

I have tried but I can’t get it to work (I’m a no-coder :slightly_smiling_face: ) I have just copied it but I don’t know which text I should change to connect it to my base.

This it how I have written it:

const project = await input.recordAsync("Pick a project", ProjectsTable)

const number = await input.textAsync("How many productions to add")

const values = []

while (i < number) {values.push({fields: {"Projects": {id: project.id}}})}

const newRecords = await ProductionsTable.createRecordsAsync(values)

To write to a Record Link field, the format has to be an array. So try the following instead:

while (i < number) {values.push({fields: {"Projects": [{id: project.id}]}})}

Mike_Pennisi
7 - App Architect
7 - App Architect

@Mathias_Elmose Your script is so close, probably you’ve already finished it. Here’s a complete version, just in case you (or anyone following along out there) still need a hand:

Click to expand
/**
 * 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: '',
    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 parentRecord = await input.recordAsync('Parent record', parentTable);
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 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 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');
Colleen_Brady
5 - Automation Enthusiast
5 - Automation Enthusiast

Thank you @Mike_Pennisi for the snippet.

I am trying to use it and seeing an error.

Event-Social-Media-Command-Center-Social-Posts-Airtable

@Colleen_Brady The Scripting Block defines a global API that includes the input object; it looks like that object isn’t available. Notice how the editor is highlighting all the references to input; probably if you hover your mouse over those, the editor will say more.

It might be that you aren’t using the Scripting Block (or at least, not the same Scripting Block that I’ve been using). The user interface surrounding the code doesn’t match what I see in my browser:

2020-09-25-scripting-block

Is this your first time using the Scripting Block? Can you say a little bit about how you installed it into your Airtable Base?

@Colleen_Brady & @Mike_Pennisi

From the screenshot, it looks like Colleen is in the Automations scripting action editor, not the Scripting Block (App).

Colleen, Automations are not capable of using the input API that is available in the Scripting App. The input API is for users to interact directly with the Scripting App, toggling selectors or typing in data. This is not something you do with an Automation - they are meant to run without any user input, thus no input API.

Colleen_Brady
5 - Automation Enthusiast
5 - Automation Enthusiast

Thank you both @Jeremy_Oglesby and @Mike_Pennisi. I have been starting to use features that I had not previously tried.

Hi,

This works fast! Thank you for sharing.

I have linked this script to a button, and it then opens the script to enter the number of records to create. However, the number of records I want to create, is already stated as a value in a foumula field in the parent record. Is it possible to get this automated in the script?