New Script for multiple templates

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

1 Like

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 Community Forum

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"}
        })
    }
}))
2 Likes