Help

Airtable Cobuilder is here! Learn more about our new no-code app creation feature, powered by AI on the Airtable Academy

New Script for multiple templates

Topic Labels: Scripting extentions
10300 28
cancel
Showing results for 
Search instead for 
Did you mean: 
Kendall_Shortt
6 - Interface Innovator
6 - Interface Innovator

Hi,

Could someone demo this script showing how to properly create the parent/child and template tabs to utilize this script?

I am having difficulty getting it to properly work.

Thanks.

28 Replies 28

Taylor, it worked perfectly! You’ve been a lifesaver again!

Jarvis
7 - App Architect
7 - App Architect

Hi, for convenience I’m resharing the scripts that Taylor gave, with some small modifications I made to match all the fields properly. My use case and details of the table are here: New Script for multiple templates - Development and integrations / Scripting app - Airtable Communit...

For scripting app

// Get a hold of your projects table.
const projectsTable = base.getTable("Projects");

// Let the user pick a record from the Projects table to generate tasks
let projectRecord = await input.recordAsync("Select a Project to generate Tasks", projectsTable)

// Get the values from the Videos and Images fields for that record
const numVideos = projectRecord.getCellValue("Videos")
const numImages = projectRecord.getCellValue("Images")

if (numVideos == null || numImages == null) {
    throw new Error("Make sure you input a number for the number of Images and Videos required!")
}

// Get a hold of your tasks table.
const tasksTable = base.getTable("Tasks")

// Create all of the Images tasks in the tasks table
for (let i = 1; i <= numImages; i++) {
    await tasksTable.createRecordAsync({
        "Task Name": `Images ${i}`, // This will generate names like "Images 1", "Images 2", etc.
        "Task Type": {name: "Images"}, // Since this is a single select field, you set it by passing an object with "Name" equal to the tag you want
        "Project": [{id: projectRecord.id}], // Since we're setting a linked record field here, we need to pass an array of objets, with the "id" key set to the id of the record we want to link to.
        "Status": {name: "Open"}
    })
}

// Create all of the video tasks in the tasks table
for (let i = 1; i <= numVideos; i++) {
    await tasksTable.createRecordAsync({
        "Task Name": `Videos ${i}`, // This will generate names like "Videos 1", "Videos 2", etc.
        "Task Type": {name: "Videos"}, // Since this is a single select field, you set it by passing an object with "Name" equal to the tag you want
        "Project": [{id: projectRecord.id}], // Since we're setting a linked record field here, we need to pass an array of objets, with the "id" key set to the id of the record we want to link to.
        "Status": {name: "Open"}
    })
}

For automation:

// Get a hold of your projects table.
const projectsTable = base.getTable("Projects");

// Get a hold of your tasks table.
const tasksTable = base.getTable("Tasks")

const projectRecordsQuery = await projectsTable.selectRecordsAsync();
const allProjectRecords = projectRecordsQuery.records;

// Iterate over all project records in the base.
Promise.all(allProjectRecords.map(async (projectRecord) => {

    // Get the values from the Videos and Images fields for that record
    const numVideos = projectRecord.getCellValue("Videos")
    const numImages = projectRecord.getCellValue("Images")

    if (numVideos == null || numImages == null) {
        throw new Error("Make sure you input a number for the number of Images and Videos required!")
    }

    // Create all of the video tasks in the tasks table
    for (let i = 1; i <= numVideos; i++) {
        await tasksTable.createRecordAsync({
            "Task Name": `Videos ${i}`, // This will generate names like "Videos 1", "Videos 2", etc.
            "Task Type": {name: "Videos"}, // Since this is a single select field, you set it by passing an object with "Name" equal to the tag you want
            "Project": [{id: projectRecord.id}], // Since we're setting a linked record field here, we need to pass an array of objets, with the "id" key set to the id of the record we want to link to.
            "Status": {name: "Open"}
        })
    }

    // Create all of the Images tasks in the tasks table
    for (let i = 1; i <= numImages; i++) {
        await tasksTable.createRecordAsync({
            "Task Name": `Images ${i}`, // This will generate names like "Images 1", "Images 2", etc.
            "Task Type": {name: "Images"}, // Since this is a single select field, you set it by passing an object with "Name" equal to the tag you want
            "Project": [{id: projectRecord.id}], // Since we're setting a linked record field here, we need to pass an array of objets, with the "id" key set to the id of the record we want to link to.
            "Status": {name: "Open"}
        })
    }
}))
Jazmin_Poyser
6 - Interface Innovator
6 - Interface Innovator

Hi @Taylor_Savage , I have read through this thread in detail and have a working Script for Creating records for multiple templates. However, I am having trouble adding an additional field to the script.

I have followed the instructions in the script, and done a number of trial and errors, but I’m stuck hoping that you or someone may be able to help.

The field I am trying to add is ‘category’, which is effectively the text field name of the Type table (Template Type). I am trying to add that to the Child table.

I have adjusted the table names for the purposes of these screenshots:

In the Parent table, the ‘category’ field is a linked record to Type Table. A second part to my question is that the use case requires multiple ‘categories’ (Template Types) to be selected here, prior to clicking the orange button. At the moment we can only select one at a time.

image

image

In the Child table, I tried both text and single select fields. Because the error I was getting says 'invalid cell value, that the must be a string.

image
image

In the Template Table, the ‘category’ field is a linked record to Type Table. But I also tried with a lookup to try and use a string rather than linked record. Neither seemed to work?

image

The following is what I added to the Script to try and add the category field.
From line 87:

let types = typesRecords.map(c => ({
    'child': [c],
    'childName': c.getCellValue(childFieldInTemplate),
    'templateTypes': c.getCellValue(templateType).map(x => x.id),
    'templateOrder': c.getCellValue(templateOrder),
    'category': c.getCellValue('category')
    // Add additional template fields here and in section below using format below.
    // Field names within c.getCellValue parentheticals should match field names in template table
    //  'templatePhase':c.getCellValue('Phase'),
    //  'templateDays': c.getCellValue('Days')
})).filter(x => x.templateTypes.includes(parentType[0].id))

from line 100

let createRecords = types.map(c => ({
    fields: {
        [childNameInChild.name]: c.childName,
        [parentFieldInChild.name]: [selectedEvent],
        [childOrder.name]: c.templateOrder,
        ['category']: c.category

I tried adding the following to the script from line 71 but that didn’t work either:

let category = settings.category;

Can you see where the problem is? Or how to adjust the script to solve for this?
Would really appreciate any guidance you can provide.

Many thanks,
Jazmin

Jazmin_Poyser
6 - Interface Innovator
6 - Interface Innovator

@Taylor_Savage is there a follow on video from the ‘walkthrough video’ linked above to show how to copy across more fields from the template table to the child table? I’m having trouble with this, primarily because in my template table (the one that corresponds to your Tasks Template in your video) is has multiple linked records/template types.

@Taylor_Savage Can the script posted here on Feb 19 be revised to include an option for a Single Select field? I’m running into an issue copying the Single Select field from my Template Table onto the Child Table.

Ariella_Faitels
4 - Data Explorer
4 - Data Explorer

Hitting this error…can anyone help? I used the Scripting app for it.

Screen Shot 2022-03-16 at 6.06.25 PM

Please reshare the link to the walkthrough video. Thanks!

Hello there. Is it possible for us to get access on the walkthrough video regarding this script? Thank you.

IronDudeMan
5 - Automation Enthusiast
5 - Automation Enthusiast

For those of you (like me) that really wanted to better understand this script but was bummed that the video link was dead... hopefully this helps.