Help

Re: Creating Projects/Tasks From Templates via Scripting Block

4152 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Giovanni_Briggs
6 - Interface Innovator
6 - Interface Innovator

A common theme we are seeing with project management bases is the desire to have project and task templates. When you create a new project, it can then generate the corresponding set of tasks, and maintain the basic details about those tasks, including the relationships and dependencies amongst those tasks.

This is similar to Creating Tasks from a Template with a few differences. When creating projects, we often find we create a new record in our projects table, and then want to link it to a set of metadata (a project template). From there, each project template should know its associated set of tasks. This allows us to add more project templates over time, and change the set of tasks required to complete each of those projects.

Additionally, the script will ensure that the dependencies between the task templates are maintained when creating the tasks for a new project.

You can see examples of the base:

Full code below

// when a new asset is created, 
// there are a certain number of tasks that must be completed
// these tasks are dependent on the type of asset
// Given a list of project and task templates,
// when a new asset is created and has a project template assigned
// we can then create corresponding set of tasks for that asset

// define some of our initial variables
// these are the basic table, field and view names to define to create the script
const project_table_name = 'Projects';
const new_project_view_name = 'New Projects';
const project_template_link_field_name = 'Project Template';

const task_table_name = 'Tasks';
const task_project_link_field_name = 'Projects';
const task_dependency_field_name = 'Followed By';
const task_primary_field_name = 'Task Name';

const task_to_template_link_field_name = '_task_template_id';   // this is used to assist with creating the links between tasks after they are created.  We don't need to maintain a true linked record relationship, but temporarily storing the template's ID is helpful

const project_template_table_name = 'Project Templates';
const task_template_table_name = 'Task Templates';
const proj_temp_task_temp_link_field_name = 'Tasks';
const task_temp_dependency_field_name = 'Followed By';
const task_temp_primary_field_name = 'Task Name';

/********************************************************/
output.markdown('# Creating tasks for new assets and assigning dependencies');

// create our table objects
const project_table = base.getTable(project_table_name);
const task_table = base.getTable(task_table_name);
const project_temp_table = base.getTable(project_template_table_name);
const task_temp_table = base.getTable(task_template_table_name);

// get all new projects that have been assigned a template
// but do not yet have tasks
const new_project_view = project_table.getView(new_project_view_name);
const new_project_results = await new_project_view.selectRecordsAsync();

// pull all of our project templates and all task templates
const project_temp_results = await project_temp_table.selectRecordsAsync()
const task_temp_results = await task_temp_table.selectRecordsAsync();

// build a map of projects to tasks
output.markdown('### Setting up');
output.markdown('Building map of project templates and task templates');
var project_task_temp_map = {};
for (let r of project_temp_results.records) {
    let temp_tasks = r.getCellValue(proj_temp_task_temp_link_field_name);

    project_task_temp_map[r.id] = temp_tasks.map((t)=>{
        return t.id;
    }) 
}
output.inspect(project_task_temp_map);

// also build a map of task template to task template so we can resolve dependencies
output.markdown('Creating map of templated tasks to dependent tasks');
var task_temp_dependency_map = {};
for (let r of task_temp_results.records){
    let next_task = r.getCellValue(task_temp_dependency_field_name);
    if(next_task !== null) {
        task_temp_dependency_map[r.id] = next_task[0].id;
    } else {
        task_temp_dependency_map[r.id] = null;
    }
}
output.inspect(task_temp_dependency_map);

// for each new project
// get the set of tasks and create the creation payloads
// we will need to do a second pass of all of these records to then _update_
// the tasks with the corresponding dependencies
//
// THIS IS THE PART OF THE SCRIPT WHERE YOU ASSIGN WHAT DATA YOU WANT IN YOUR NEWLY CREATED TASKS
//
output.markdown('### Creating new tasks')
output.markdown(`Found **${new_project_results.records.length}** projects which need task assignment`)
var payloads = [];
for(let r of new_project_results.records){
    // there should only ever be one project template linked
    // so just take the first one
    let p_id = r.getCellValue(project_template_link_field_name)[0].id;

    let task_temp_ids = project_task_temp_map[p_id];
    let task_temps = task_temp_ids.map((i)=>{
        return task_temp_results.getRecord(i);
    });
    for(let t of task_temps) {

        payloads.push({
            fields: {
                [task_project_link_field_name]: [{id: r.id}],
                [task_primary_field_name]: t.getCellValueAsString(task_temp_primary_field_name),
                [task_to_template_link_field_name]: t.id
            }
        });
    }
}

// create all of the new tasks
// we should hold on to the created IDs
output.markdown(`Creating **${payloads.length}** tasks across these projects`);
var new_tasks = [];
while(payloads.length > 0){
    let n = await task_table.createRecordsAsync(payloads.slice(0,50));
    new_tasks = [...new_tasks, ...n];
    payloads = payloads.slice(50);
}

output.markdown('### Creating dependencies between new tasks');
output.markdown('Pulling newly created tasks')
// refetch these tasks so that we can update the dependencies
const task_results = await task_table.selectRecordsAsync({
    fields: [
        task_primary_field_name, 
        task_project_link_field_name, 
        task_to_template_link_field_name, 
        task_dependency_field_name
    ]
});

// pull out only the newly created tasks
const tasks_to_update = new_tasks.map((t)=>{
    return task_results.getRecord(t);
});

output.markdown('Creating map of new tasks to templated task ids to resolve dependencies')
// create a map of new task to templated task ids
// and templated tasks to new task ids
// group them by project since any given project may be using the same set of templated tasks
var new_task_to_template_map = {};
for(var r of tasks_to_update){
    let p = r.getCellValue(task_project_link_field_name)[0].id;
    let temp_t = r.getCellValue(task_to_template_link_field_name);
    if(new_task_to_template_map[p] === undefined){
        new_task_to_template_map[p] = {
            task_to_template: {},
            template_to_task: {}
        };
    }
    new_task_to_template_map[p].task_to_template[r.id] = temp_t;
    new_task_to_template_map[p].template_to_task[temp_t] = r.id;
}
output.inspect(new_task_to_template_map);

// now go back through the tasks one more time and actually build out the payloads to establish links
// not all tasks will have a dependent task
// we can filter these out afterwards
payloads = tasks_to_update.map((r)=>{
    let p = r.getCellValue(task_project_link_field_name)[0].id;
    let temp_task = r.getCellValue(task_to_template_link_field_name);
    let temp_task_dependency = task_temp_dependency_map[temp_task];
    let dependent_task_id = new_task_to_template_map[p].template_to_task[temp_task_dependency];
    if(dependent_task_id === undefined) {
       return undefined;
    }

    return {
        id: r.id,
        fields: {
            [task_dependency_field_name]: [{id: dependent_task_id}]
        }
    }
}).filter((r)=>{
    return r !== undefined;
});
output.inspect(payloads);

output.markdown(`Updating **${payloads.length}** tasks with required dependencies`);
while(payloads.length > 0) {
    await task_table.updateRecordsAsync(payloads.slice(0,50));
    payloads = payloads.slice(50);
}
output.markdown(`### Done`);
34 Replies 34
Kristi_Smith
5 - Automation Enthusiast
5 - Automation Enthusiast

This script is wonderful, thank you for sharing @Giovanni_Briggs! I’ve got it functioning properly in my base.
However, I’d like to add the ability to calculate due dates for tasks based on certain criteria. My projects table has 5 different date fields and my tasks all correspond to one of the 5 fields.

Is there a way to map a single select field in the task template table with the corresponding dates the the record in the project? And then use a number field for the task in the task template table to calculate the due date for the task and output it into the task table?

It would be so much easier if I could access information from tables across the base and still use formulas as well!
Essentially for the added record, it would be IF(task type single select in tasks template table === A) use reference date A from projects table
then calculate due date using WORKDAY((date A from projects table), (value of number field in tasks template table), days)

Is there a way to do this or does anyone know of a script block that I can reference to add this in?

Hi guys,

I am trying to run this script using button. Which part of the script that I should modify?

Alivia_Smith
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello
I got this to work but I’m also looking for a way to pass a number over from the template to the tasks, so I can set start dates and end dates based on how long a task is going to take.
I have number fields in both bases and it showed up an error

j: Can’t create records: invalid cell value for field ‘Time’.
Cell value has invalid format: must be a number
at main on line 101

How can I arrange the code below to copy over a number?
[task_time_field_name]: {name: t.getCellValueAsString(task_temp_time_field_name)}

I’m assuming I need to change the ‘get Cell Value as String’ but I don’t know what goes there…
Thanks!

Steve_Hazeltine
4 - Data Explorer
4 - Data Explorer

@Giovanni_Briggs This script is exactly what I need, thank you! There is one problem I’m running into that I’m wondering if you have any thoughts on.

The tasks that are linked to my new project are not in the order that they were in, in the template. Any thoughts on how to fix that?

Hi Steve, did you figure out this issue? I'm having the same challenge, my tasks seem to be scrambled and it would save me time if the order could be followed through from the template.