Help

Creating Projects/Tasks From Templates via Scripting Block

Topic Labels: Scripting extentions
18704 34
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
Jason_Friedlan1
6 - Interface Innovator
6 - Interface Innovator

Giovanni:

This is awesome. Would you be able to help me tweak the script so that it takes the Task Name and allows me to update a single select with it? All task names already = the choices in my single select.

Jessica_Plautz
4 - Data Explorer
4 - Data Explorer

I’m trying to implement this but in my own base keep getting the message:

TypeError: temp_tasks is null
at main on line 52

What might I be missing from my base that would cause the error? I recreated all of the same fields.

Also, is there a way to have a sequence of dates tied to each task? Like you provide a launch date and the script then does following dates moving forward based on the field of days to complete?

It’s likely because the project template you are linking to doesn’t have any tasks assigned to it. When you do r.getCellValue(field_name) on a linked record field, it returns “null” if the field is blank (and you can’t iterate over null).

At line 52, you can make an update that handles this case:

 let temp_tasks = r.getCellValue(proj_temp_task_temp_link_field_name);
 temp_tasks = temp_tasks === null ? [] : temp_tasks;

This script copies very little data from the task template over to the “live” tasks right now, but it’s easy to add. Lines 80-105 is the area where you are building the logic for what data in your task templates you want to pull over to the Tasks table.

Chris_Guthrie
7 - App Architect
7 - App Architect

Holy cow, this is an awesome script. I’m diving in today. I’ve been trying to conceive of basically the same thing. Thanks @Giovanni_Briggs!

Chris_Guthrie
7 - App Architect
7 - App Architect

Question for you @Giovanni_Briggs. I see a Record ID field in the Tasks table. What function doe this field serve. When I use the script the field does not auto-populate. Is it supposed to?

Edit: When I make a copy of your base and then test it, the Record ID field is populating. But in my base where I’ve recreated all fields and views, it is not.

Ah, I didn’t re-create it as I thought I had. The Record ID field is a formula, and I had it as a Simple Text. Fixed and working now.

I’m wondering if you could enlighten me to figure out how to make a few other fields from the Task Template table copy over to the Tasks table? I would like a “task description” field (Long Text), “View URL” field (URL field type), and “Go to View” field (button) field populate in the tasks tables upon project creation.

Jason_Friedland
5 - Automation Enthusiast
5 - Automation Enthusiast

Giovanni:

This is awesome. Would you be able to help me tweak the script so that it takes the Task Name and allows me to update a single select with it? All task names already = the choices in my single select.

// 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 = ‘MP Projects’;
const new_project_view_name = ‘New Projects’;
const project_template_link_field_name = ‘Project Template’;

const task_table_name = ‘MP Deliverables’;
const task_project_link_field_name = ‘Project’;
const task_dependency_field_name = ‘Followed By’;
const task_primary_field_name = ‘Deliverable Types’;

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 = ‘Deliverable Types’;

/********************************************************/
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(®=>{
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(®=>{
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);

Tom_Glatt
6 - Interface Innovator
6 - Interface Innovator

Question About Linked Records/Array

Really nice script and example, and I have it working as intended - but I have a question about tweaking the structure. It is helpful to link back to the template tasks - for example if we want greater details about the tasks than we want to also store in the task assignment table.

The problem I’ve run into is that as written the script doesn’t put the task names in an array - so the script generates an error if you try to add the task names to a linked record field.

Im not certain I understand how to change this behavior, and the community guidance I’ve read about setting names in an array is confusing to me. Any suggestions how to do this, or links to clear info (for a newbie) that describes how to use arrays in the script block?

Giovanni any way to help?