I would like to ask for some help with a script. We have a ton of automations for templating inside our workspace. For example:
When an order with Product X comes in and Product X has a template with Tasks of templates we create normal tasks. As an exact duplicate from those Tasks of templates (in Dutch we call this Taken van templates).
The only challenge we have left is to get the Rich Text description from Tasks of templates into the normal task.
At this moment we use a seperate automation to copy Description (= Omschrijving in Dutch) from Tasks of templates to de Description from Tasks. Therefore we use the following script:
const fibery = context.getService('fibery');
for (const destination of args.currentEntities) {
const source = await fibery.getEntityById('DB templates/Taken van templates', destination['Taken van templates']['Id'], ['Omschrijving']);
const sourceContent = await fibery.getDocumentContent(source['Omschrijving']['Secret'], 'html');
await fibery.appendDocumentContent(destination['Omschrijving']['Secret'], sourceContent, 'html');
}
Since we have many automations for templating, it would be great if this script could be modified and added to the automation where we create those tasks.
Copying the script 1 to 1 does not work (obviously), but I have no knowledge of Scripts. As a result, I’m unable to modify the script. I hope there’s someone who’s willing to help me out.
Some aditional information about the field and database names.
The database of the automation where I want to add the script to is Taken van templates.
The Space is called DB templates
Taken van templates has a many - one relation with Taken.
Database Taken van templates is located inside the space DB templates. Database Taken is located inside the space DB basis
The Description field is called ‘Omschrijving’ in both databases of templates and tasks.
If I understand you, you want to ‘push’ the Description contents from the Taken van templates entity to the newly-created Taken entity, as an action to follow the Add Taken item action, is that right?
If that’s the case, then I think your script will need to make use of args.steps which is the array of results of previous actions steps in the automation.
Looking at the screenshot, the action to create the new Taken item is step 3, so I think your script would need to look something like this:
const fibery = context.getService('fibery');
for (const source of args.currentEntities) {
// get the content of the Template Description
const sourceContent = await fibery.getDocumentContent(source['Omschrijving']['Secret'], 'md');
// get the Id of the Task created in step 3
const destinationId = args.steps[3].result.entities[0]['Id'];
// get the Task, including Description field
const destinationTask = await fibery.getEntityById('DB templates/Taken', destinationId, ['Omschrijving']);
// copy the source Description to the Task's Description
await fibery.setDocumentContent(destinationTask['Omschrijving']['Secret'], sourceContent, 'md');
}
The automation is not working yet. I think it is because this part is not right yet.
Which Space/Database should I refer to? The destination (DB basis/Taak) or the template (DB templates/Taken van template). Both of them are not working unfortunately
At first I thought the script wasn’t working, but just found out it does. But only when a template contains 1 task.
In the first test the template did have several tasks. In order to provide you a nice and clean setup I’ve made some changes (→ cut back the number of Tasks of templates to just 1), and run the automation once more. At that point, the script is working.
Then I added a second task, one without a description (see image → this is the template settings). At that point the script isn’t working anymore. The tasks are being created, but without the description.
Is there a way to modify the script in a way that so that for every task which is being created the Omschrijving from the linked Taken van templates is being added.
The script I’m currently using is.
const fibery = context.getService('fibery');
for (const source of args.currentEntities) {
// get the content of the Template Description
const sourceContent = await fibery.getDocumentContent(source['Omschrijving']['Secret'], 'html');
// get the Id of the Task created in step 3
const destinationId = args.steps[3].result.entities[0]['Id'];
// get the Task, including Description field
const destinationTask = await fibery.getEntityById('DB basis/Taak', destinationId, ['Omschrijving']);
// copy the source Description to the Task's Description
await fibery.setDocumentContent(destinationTask['Omschrijving']['Secret'], sourceContent, 'html');
}
As you can see, I’ve changed ‘md’ into ‘html’ and set the right space/database.
When an Trigger from orderregel is linked to Taken van templates the task is being created. So yes. There is only one tasks being created per ‘entity is linked to’.
But still, when I have one task inside the template, everything is working as it is supposed to. When I have 2 tasks inside my template (so Trigger from orderregel is linked to 2 Taken van templates) the script isn’t working anymore. I don’t receive any errors inside the automation, but the result in Omschrijving is blank.
I have absolutely no idea why this leads to a different result → Empty description.
Note: We also need to keep in mind that it might happen that 2 orders are being placed at the same time. So more than 1 orderregel can be linked to Taken van templates.
Don’t think this is an issue, because new tasks are being created for every orderregel linked to, but mentioned it, just to be shure.
Are you saying that the automation that contains the action to create a task (and the action to populate the task description using a script) is running twice, but is only populating the text the first time it runs?
Just so I understand, are the tasks related to the template being created in the same way?
If you want to debug what’s going on, try adding some logging to the script:
const fibery = context.getService('fibery');
for (const source of args.currentEntities) {
console.log(source);
// get the content of the Template Description
const sourceContent = await fibery.getDocumentContent(source['Omschrijving']['Secret'], 'html');
console.log(sourceContent);
// get the Id of the Task created in step 3
const destinationId = args.steps[3].result.entities[0]['Id'];
console.log(destinationId);
// get the Task, including Description field
const destinationTask = await fibery.getEntityById('DB basis/Taak', destinationId, ['Omschrijving']);
console.log(destinationTask);
// copy the source Description to the Task's Description
await fibery.setDocumentContent(destinationTask['Omschrijving']['Secret'], sourceContent, 'html');
}
There was a mistake in the code I supplied: it assumed that the automation was running for a single Template, so was always copying the text from the same Template each time. In your case, one of the Templates had an empty description, and it just happened to be this one being copied each time.
The corrected code looks like this
const fibery = context.getService('fibery');
for (const [i, source] of args.currentEntities.entries()) {
// get the content of the Template Description
const sourceContent = await fibery.getDocumentContent(source['Omschrijving']['Secret'], 'html');
// get the Id of the Task created in step 3
const destinationId = args.steps[3].result.entities[i]['Id'];
// get the Task, including Description field
const destinationTask = await fibery.getEntityById('DB basis/Taak', destinationId, ['Omschrijving']);
// copy the source Description to the Task's Description
await fibery.setDocumentContent(destinationTask['Omschrijving']['Secret'], sourceContent, 'html');
}