Help

Re: Automate Task Creation in projects via a script

Solved
Jump to Solution
2877 1
cancel
Showing results for 
Search instead for 
Did you mean: 
xXTMXx
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello everyone, i have a set of tasks that once a lead moved from one stage to another those tasks should be created. The set of tasks already listed in a Table called "Tasks Reference", content of the table as below:

xXTMXx_0-1673289209521.png

Linked with another table called "Tasks Stages Reference", content of table as below:

xXTMXx_1-1673289370127.png

"Tasks Stages Reference" this table is have all possible stages that the lead can go through.

I also built a junction table that will have the tasks that will be created for any lead, called "To Do's", as below:

xXTMXx_2-1673289679298.png

Here's the script:

let inputVariables = input.config() 
let lead = inputVariables.leadName
let stage = inputVariables.stageName

let leadTrackingT = base.getTable('Lead Tracking');
let tasksT = base.getTable("To Do's");
let stagesTemplateT = base.getTable('Tasks Stages Reference');
 

let stageTasks = [];
if (stage) {
 for (let task of stage.getCellValue('Tasks Reference')) {
   stageTasks.push(
       {
           fields: {
               'Task': task.name,
               'Lead': [ {id: lead.id} ]
           }
       }
   )
 }
}
while (stageTasks.length > 0) {
   await tasksT.createRecordsAsync(stageTasks.slice(0, 50));
   stageTasks = stageTasks.slice(50);
}

Once i test this script an error keeps coming as below:

xXTMXx_3-1673290181950.png

below are the inputs: 

xXTMXx_4-1673290238513.png

xXTMXx_5-1673290282010.png

 

 

1 Solution

Accepted Solutions
xXTMXx
5 - Automation Enthusiast
5 - Automation Enthusiast

OK, i adjusted the script and now the tasks are created successfully. Here's the script were automation will make pre-defined tasks created automatically in "Work Items" table for specific lead/project. below is the final script that worked for me:

let lead = input.config();

// Find template action items
let templateActionItemsTable = base.getTable("[HIDE] Action Items");
let allTemplateActionItems = await templateActionItemsTable.selectRecordsAsync({fields: templateActionItemsTable.fields});

let templateActionItems = allTemplateActionItems.records.filter(templateActionItem => {
    // check wether action items matching request
    return templateActionItem.getCellValueAsString("Stages") === lead.stageName;
})

// create template action items in work items table

let actionItems = templateActionItems.map(templateActionItem => {
    return {
        fields: {
            "action-item": templateActionItem.getCellValue("Action Items"),
            "Stage": {
                name: lead.stageName
            },
        }
    }
})

let workItemsTable = base.getTable("Work Items");
await workItemsTable.createRecordsAsync(actionItems);

 

See Solution in Thread

5 Replies 5

`getCellValue` should be used on a record selected via `selectRecordAsync()` or `selectRecordsAsync()` first right?

Thanks for your reply. I tried it before and similar error appeared as below:

xXTMXx_0-1673346671928.png

 

I think maybe it's something related to the input as string.

Nah, you have to do a `table.selectRecordsAsync`to get the records, and then per record you use `getCellValue`

I think you may want to use one of Airtable's existing scripts and modify it from there as that would help a lot

xXTMXx
5 - Automation Enthusiast
5 - Automation Enthusiast

@TheTimeSavingCo  and @everyone this is the script I got so far after rewrite it :

 

let lead = input.config();
let stageName = lead.stageName;
let leadName = lead.leadName;
let leadID = lead.recordId
let assignee = lead.assginee;

let templateActionItemsTable = base.getTable("[HIDE] Action Items");
let allTemplateActionItems = await templateActionItemsTable.selectRecordsAsync({fields: templateActionItemsTable.fields});

let templateActionItems = allTemplateActionItems.records.filter(templateActionItem => {
    // check wether action items matching request
    return templateActionItem.getCellValueAsString("Stages") === lead.stageName;
})

console.log(templateActionItems);

// create template action items in work items table

let actionItems = templateActionItems.map(templateActionItem => {
    return {
        fields: {
            "action-item": templateActionItem.getCellValue("Action Items"),
            "Stage": {
                name: lead.stageName
            },
             "Lead": [
                 {id: lead.recordId},
                 {name: lead.leadName},
              ],
             "Assignee": [
                {id: lead.assigneeId},
             ]
        }
    }
})


console.log(actionItems);

let workItemsTable = base.getTable("Work Items");

await workItemsTable.createRecordsAsync(actionItems);

 

Amendments were done and the script got updated after learning a couple of new things.

now the error showing is as below:

xXTMXx_1-1673825637277.png

The error is coming even that the log is showing information that somehow i think correct.

This field is a linked record called "Lead" from another table called "Company". 

Input were updated as well as the below:

xXTMXx_1-1673824467435.png

xXTMXx_2-1673824493927.png

xXTMXx
5 - Automation Enthusiast
5 - Automation Enthusiast

OK, i adjusted the script and now the tasks are created successfully. Here's the script were automation will make pre-defined tasks created automatically in "Work Items" table for specific lead/project. below is the final script that worked for me:

let lead = input.config();

// Find template action items
let templateActionItemsTable = base.getTable("[HIDE] Action Items");
let allTemplateActionItems = await templateActionItemsTable.selectRecordsAsync({fields: templateActionItemsTable.fields});

let templateActionItems = allTemplateActionItems.records.filter(templateActionItem => {
    // check wether action items matching request
    return templateActionItem.getCellValueAsString("Stages") === lead.stageName;
})

// create template action items in work items table

let actionItems = templateActionItems.map(templateActionItem => {
    return {
        fields: {
            "action-item": templateActionItem.getCellValue("Action Items"),
            "Stage": {
                name: lead.stageName
            },
        }
    }
})

let workItemsTable = base.getTable("Work Items");
await workItemsTable.createRecordsAsync(actionItems);