I’m working on a script to create tasks from task templates without having to link the templates to the tasks. I’ve encountered an error and I’m not sure why categoryWithTemplates returns the linked entities with no error.
taskTemplate throws the Failed to execute Action “Script”: getEntityById: type is not defined error.
Here’s the script:
const fibery = context.getService('fibery');
// Get the current category entity and its linked task templates
const categoryEntity = args.currentEntities[0];
const categoryWithTemplates = await fibery.getEntityById(categoryEntity.type, categoryEntity.id, ['Task Templates']);
console.log(categoryWithTemplates);
// Prepare an array for batch creation of new tasks
const newTasks = [];
// Iterate over each task template linked to the category
for (const template of categoryWithTemplates['Task Templates']) {
// Fetch the task template details including its type
const taskTemplate = await fibery.getEntityById(
template.type,
template.id,
['Name', 'Assignees', 'Description']
);
// Create a new task based on the task template
newTasks.push({
Name: taskTemplate.Name,
Assignee: taskTemplate.Assignees.length > 0 ? taskTemplate.Assignees[0].id : null,
Description: taskTemplate.Description
});
I believe the “type” field (or “Type”) is only defined for args.currentEntities
, but not for any entities that you retrieve from the API.
Please check this using console.log
to verify.
2 Likes
Spot on here! When I swapped template.type for ‘Templating/Task Template’ the error went away.
For anyone who may be interested in doing something similar in the future here’s the final script.
const fibery = context.getService('fibery');
// Get the current category entity and its linked task templates
const categoryEntity = args.currentEntities[0];
const categoryWithTemplates = await fibery.getEntityById(
categoryEntity.type,
categoryEntity.id,
['Task Templates']
);
// Retrieve linked task templates
const linkedTaskTemplates = await fibery.getEntitiesByIds(
'Templating/Task Template',
categoryWithTemplates['Task Templates'].map(t => t.id),
['Name', 'Assignees', 'Description template', '⭐']
);
const assigneeLinks = [];
// Iterate over each task template
for (const template of linkedTaskTemplates) {
// Create a new task directly
const newTask = await fibery.createEntity('Client Management/Task', {
Name: template.Name,
'⭐': template['⭐']
});
// Populate the task description if the template has one
if (template['Description template']) {
const descriptionSecret = template['Description template'].Secret;
const descriptionContent = await fibery.getDocumentContent(descriptionSecret, 'md');
await fibery.setDocumentContent(newTask['Description'].Secret, descriptionContent, 'md');
}
// Handle assignees if present
if (template.Assignees && template.Assignees.length > 0) {
template.Assignees.forEach(assignee => {
assigneeLinks.push({
id: newTask.id, // The newly created task's ID
itemId: assignee.id // The assignee's ID
});
});
}
}
// Link assignees
if (assigneeLinks.length > 0) {
await fibery.addCollectionItemBatch('Client Management/Task', 'Assignee', assigneeLinks);
}
1 Like