Help

Re: Script Question: Linking Records

1799 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Lisa_Bauer
6 - Interface Innovator
6 - Interface Innovator

I am using this app that @Alex.Wolfe published (THANK YOU ALEX)! , and am hoping to make some tweaks so that I can use linked records fields.

Right now the app allows you to create one record in another table for each option in a multiple select field in the source table, and links the new records back to the source record.

Which is exactly what I need, but I believe it’s set up so the options from the multiple select cannot be outputted into a linked records field in the second table.

Here’s my used case scenario:

  1. People will submit a request to have a project staffed by multiple designers.
  2. On this request they will select multiple types of designers (this is the multi-select field; i.e. a graphic designer, a mechanical engineer and an industrial designer).
  3. Based on what they have selected, I am using this script to create a new record for each of the types (i.e. a record for a graphic designer linked back to the request, a record for a mechanical engineer linked back to the request and a industrial designer linked back to the request)

What I want to change:

  • I have a field in this second table (where the new records are being created) that links to another Table (Designers’ Table) and would love the new records to use that linked field rather than a single text field that I have to set automations for to link back to the designers’ table.
  • Be able to use this in an Airtable automation, so removing the interactive input/output and using static data instead.

Is this possible? Thank you for your help!

4 Replies 4
Alex_Wolfe1
Airtable Employee
Airtable Employee

Hi Lisa :wave:

First, glad to hear the script is helpful!

Second, there definitely is a way to use linked records instead. It just requires some slightly different code. This example base in our Universe has one way of doing it built in using a Scripting extension.

To answer your question around Automations (from DM)
I’ve added a version of my script (see below) for this you can use to adapt to your base. Note you’ll need to add some inputs from the Automation Trigger, to tell it which record to use (the record you want to create deliverables for).

Here’s how to setup the Automation
In your Automation, you’ll want to add input variables (left-hand side of the Automation Run a Script Action window) for the following:

  1. deliverablesToCreate => this should be the multiple select field values inserted from your trigger step
  2. recordID => this should be the Airtable record ID value inserted from your trigger step

Here’s the Automation-friendly version of the code where you’d want to update the variables before the “DO NOT EDIT” section:

let { deliverablesToCreate, recordID } = input.config(); // Values passed from the trigger record
console.log(deliverablesToCreate); // Log to console

let delivField = 'Deliverables'; // Field name or ID of multiple select field

let tableDest = base.getTable('tblXXXXXXX'); // Destination table name or ID
let destinationField = 'Name'; // Primary field name or ID you wish to populate the Deliverables name into
let projField = 'Projects'; // Linked record field name or ID back to source table records

//
//
// ———— * DO NOT EDIT * ————
if (deliverablesToCreate.length > 0) {

    // Create records
    let dToCreate = [];

    for (let i = 0; i < deliverablesToCreate.length; i++) {
        let name = deliverablesToCreate[i]

        dToCreate.push({
            fields: {
                [destinationField]: name,
                [projField]: [{ id: recordID }]
            }
        })
    };

    // Batches the creation
    while (dToCreate.length > 0) {
        await tableDest.createRecordsAsync(dToCreate.slice(0, 50));
        dToCreate = dToCreate.slice(50);
    }
    // Output confirmation for the user
    console.log(`${deliverablesToCreate.length} records created ✅`);

} else {

    console.log(`No deliverables chosen for this record. Make selections in the ${delivField} field first.`)

};

@Alex.Wolfe , AMAZING, thank you SO much for this!

I did get one error:

Error: Field “fldjuDEGf1Rlj0WKg” cannot accept the provided value.
at main on line 61

Any idea for what I might have done wrong?

Lisa_Bauer
6 - Interface Innovator
6 - Interface Innovator

Update: @Alex.Wolfe , I think I figured it out! I had selected the linked record field rather than the multi-select field so I think that’s what caused the error! Switching it to the mult-select field made it work perfectly. THANK YOU!

Awesome glad to hear you got this working! :raised_hands: