Script help for templating a description field

Hi all,

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.

Hope someone is able to help me out.

Thank you!

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');
}

Hi @Chr1sG

Yes, this is right!

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

It should be the space/database where the newly-created Task entity is found

Hi @Chr1sG ,

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.

image

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.
image

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.

Is there a way to modify the script to make it w

I don’t understand.
I assumed you wanted to update the rich text of the Task that was created by this action step:

There should only ever be one Task created by this action, so I don’t know what you mean when you say

The automation isn’t designed to do anything if a Task is created from a Template in any other way than via the above action.

@Chr1sG

I understand that the automation is designed to only created one task at a time. But a template can have more than one task from template. See image.

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?

Indeed, if 2 orderregels get linked, then the automation should run twice.

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');
}

Hi @Chr1sG

I will record a video of the flow and send this via email to you. So I can also add some internal links from our workspace.

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');
}

I have fixed it in your workspace :wink:

1 Like

@Chr1sG You’re an absolute hero! Thank you so much!!

1 Like