Jan 09, 2023 10:54 AM
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:
Linked with another table called "Tasks Stages Reference", content of table as below:
"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:
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:
below are the inputs:
Solved! Go to Solution.
Jan 17, 2023 06:26 AM
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);
Jan 10, 2023 02:21 AM
`getCellValue` should be used on a record selected via `selectRecordAsync()` or `selectRecordsAsync()` first right?
Jan 10, 2023 02:32 AM
Thanks for your reply. I tried it before and similar error appeared as below:
I think maybe it's something related to the input as string.
Jan 11, 2023 06:57 PM
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
Jan 15, 2023 03:16 PM - edited Jan 15, 2023 03:36 PM
@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:
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:
Jan 17, 2023 06:26 AM
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);