Help

Re: New Script for multiple templates

6412 7
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

Thanks Taylor. Much appreciated.

It appears I am still getting the same error. I have it set up exactly the same as the original script that worked fine. Maybe scripts aren’t for me.

Adnan_Ahmed
7 - App Architect
7 - App Architect

Hello @Taylor_Savage , will it be possible to use this script like this?

I have a “Projects” table with 2 additional fields - “Videos” and “Images”. As an example, what this says to me is that Project X will have a certain number of video tasks and a certain number of image tasks, and I want to create those in a new table. Here’s a screenshot of the projects table:

image

Here’s the result I want to get. I’ve added a single select field to show which type of task each one is, and then I’ve also added a “status” field which for my use case will be in “Open” for all new tasks created in this script.

image

If this is possible to create new records automatically with a script, it would save me a lot of mistakes/headaches as we end up making 700+ contents every month, so manually creating and managing these with our small team is a hassle.

Certainly! If I understand it correctly, this case is actually quite a bit more straightforward. Here’s a script for you:

// 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 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"}
    })
}

There are more efficient ways to write this script, but I tried to make it as clear to understand what’s going on as possible.

Hello Taylor, wow thank you so much! I’ll try tinkering with this. Like many others here I don’t know how to code (and try to work on examples already given), so I’m really grateful for this :grinning_face_with_big_eyes:

No problem! Tinkering/working with examples is coding! I don’t know a single professional software engineer who doesn’t copy/paste/tweak :slightly_smiling_face:

Let me know if you have any questions.

Hi @Taylor_Savage - Saw your reference to the team there working on getting a canonical version of the script published to the marketplace. Did that end up happening or is this still a work-in-progress? Thanks for your help!


Edit: just to expand on this, and to include an example… I see the script in the marketplace published in December last year “Create records for multiple templates” and what I am really hoping to accomplish here is to create sets of tasks per project that is based on the type of project it is. However, there is a common set of tasks that each project pulls from. For example “Email guests” is a template task listed once, but is assigned to multiple types of projects, and so for every project that has a type associated with it that includes the single task “Email guests”, it will create one new task specific to that project.

Example Projects (Types)…

  • New Project 1 - Homecoming Dance (Type of project: All-student event)
  • New Project 2 - Prom Dance (Type of project: Upper-classmen event)
  • New Project 3 - Alumni Reunion (Type of project: Alumni event)

Example Tasks (Types)…

  • Template Task 1 - Secure the off-campus event location (For types: Alumni event)
  • Template Task 2 - Determine Chaperones (For types: All-student event and Upper-classmen event)
  • Template Task 3 - Hire 3rd party photographer (For types: Upper-classmen event and Alumni event)
  • Template Task 4 - Email guests (For types: All-student event and Upper-classmen event and Alumni event)
  • Template Task 5 - Solicit charitable contributions (For types: Alumni event)

And the resulting output would be

  • Output Task 1: Homecoming Dance - Determine Chaperones
  • Output Task 2: Homecoming Dance - Email guests
  • Output Task 3: Prom Dance - Determine Chaperones
  • Output Task 4: Prom Dance - Hire 3rd party photographer
  • Output Task 5: Prom Dance - Email guests
  • Output Task 6: Alumni Reunion - Secure the off-campus event location
  • Output Task 7: Alumni Reunion - Hire 3rd party photographer
  • Output Task 8: Alumni Reunion - Email guests
  • Output Task 9: Alumni Reunion - Solicit charitable contributions

Thanks for your help!


Edit #2:
I originally was using this script that is in the marketplace which looked a little different than the Vimeo video tutorial above (small cosmetic changes to the script field drop-down menus). I tried updating the field type for the task template to be a multiple record-capable link to another record, and it didn’t work. But then I tried @Taylor_Savage 's script example from his Feb 19th post above, and it worked like a charm with the multiple types per task template. So, this may be what I needed, but I am going to start to really build this out now… Up until this point I was using a very simple base format setup that mostly mirrored the Vimeo example. Next, I’ll see whether the real data will work once I start populating more columns that would need to be used with each template task.
Question: Is there any plan to update the marketplace version of this script with the version that is in the thread above from Feb 19th? Or possibly any other even more updated version? :slightly_smiling_face: Thanks so much for the help.

Hello @Taylor_Savage , I’ve set everything up and it works flawlessly! Can’t thank you enough for your support.

I have another question regarding scripts - I noticed (as shown in this YouTube video) that we can trigger scripts to run automatically through automations. I followed the video, but got stuck at the last stage, where I want the automation to run the script for all Projects automatically (I’ve set the trigger to run every 24 hours as shown in the screenshot below).

image

I got confused when I realized that the original script is set up in such a way that it prompts me on each run to select the “Project”. The result I want to achieve is to create all Tasks for all Projects (or maybe for Projects meeting certain conditions, such as having “Green” status from a single-select field, + “Ongoing” status from a linked field, or for all projects in a certain view) automatically, without me having to select which Project, or without me having to click anything. Is this doable with the current automation?

Hello @Taylor_Savage . Following up on my latest message, here’s a screenshot of the error I’m getting when I’m trying to copy/paste the script into an automation. I’m guessing I’ll have to modify a few lines, and I tried to do it manually using the docs and by seeing some other messages in this board, but I’m still scratching my head :frowning:

image

Update: seeing the example on getting records (in the Examples section in the screenshot below), I managed to edit the error at line 5. Now I’m getting this error at line 8

image

Hey @BriShny - glad you got it to work! Yes, we’re planning on updating the version of this script in the Marketplace to an updated version.

Hey @Jarvis - here’s an adapted version of the above script for your Videos and Images example, that will run in an automation.

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

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

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

// Iterate over all project records in the base.
Promise.all(allProjectRecords.map(async (projectRecord) => {
    // Skip to the next project record if this one already has Tasks associated with it.
    if (projectRecord.getCellValue("Simple Tasks") && projectRecord.getCellValue("Simple Tasks").length > 0) {
        return Promise.resolve();
    } 

    // 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"}
        })
    }
}))

You’re almost there with the second example you posted.

The next step (that’s reflected in the above script) is to actually iterate over all of the projectRecords, check if they don’t yet have tasks assigned, and if not run the original code on them.