# Script help for templating a description field

**URL:** <https://community.fibery.io/t/script-help-for-templating-a-description-field/5726>\
**Category:** Get Help\
**Created:** [January 22, 2024, 9:21am UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726 "2024-01-22T09:21:14Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marloes](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/marloes/32/5926_2.png) [@Marloes](https://community.fibery.io/u/Marloes)\
**Post date:** [January 22, 2024, 9:21am UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/1 "2024-01-22T09:21:14Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [January 22, 2024, 10:02am UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/2 "2024-01-22T10:02:34Z")

</div>

> [@Marloes](#):
>
> 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.

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:

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

```

---

<div class="post-metadata">

**Author:** ![Marloes](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/marloes/32/5926_2.png) [@Marloes](https://community.fibery.io/u/Marloes)\
**Post date:** [January 22, 2024, 10:28am UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/3 "2024-01-22T10:28:31Z")

</div>

Hi @Chr1sG

> [@Chr1sG](#):
>
> 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?

Yes, this is right!

The automation is not working yet. I think it is because this part is not right yet.

> [@Chr1sG](#):
>
> ```auto
> // get the Task, including Description field
> const destinationTask = await fibery.getEntityById('DB templates/Taken', destinationId, ['Omschrijving']);
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [January 22, 2024, 11:35am UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/4 "2024-01-22T11:35:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marloes](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/marloes/32/5926_2.png) [@Marloes](https://community.fibery.io/u/Marloes)\
**Post date:** [January 22, 2024, 3:12pm UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/6 "2024-01-22T15:12:32Z")

</div>

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](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/a/a57f429bd3a1a9ce4ea51b609b2b79ba4a2e3978.png)

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](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/4/4e3ac565bad412ce2416d1960d0dbed4cfef73e4.png)

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.

```auto
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

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [January 22, 2024, 3:17pm UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/7 "2024-01-22T15:17:16Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/9/94137c1cd86242aa7761149c4ec86e9dffbc0c3d.png)

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

> [@Marloes](#):
>
> At first I thought the script wasn’t working, but just found out it does. **But only when a template contains 1 task.**

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.

---

<div class="post-metadata">

**Author:** ![Marloes](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/marloes/32/5926_2.png) [@Marloes](https://community.fibery.io/u/Marloes)\
**Post date:** [January 22, 2024, 3:43pm UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/8 "2024-01-22T15:43:31Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/3/33c9a63bd4bc6c5a43e4b2c854bf0f9c08dfc36b.png)

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

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/4/4035a9112f72087ee9842a6d9ba3588666e1a73b.png)

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.

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [January 22, 2024, 4:05pm UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/9 "2024-01-22T16:05:31Z")

</div>

> [@Marloes](#):
>
> 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.

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?

> [@Marloes](#):
>
> 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.

Just so I understand, are the tasks related to the template being created in the same way?

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [January 22, 2024, 4:06pm UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/10 "2024-01-22T16:06:37Z")

</div>

> [@Marloes](#):
>
> 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._

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

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [January 22, 2024, 4:53pm UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/11 "2024-01-22T16:53:05Z")

</div>

If you want to debug what’s going on, try adding some logging to the script:

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

```

---

<div class="post-metadata">

**Author:** ![Marloes](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/marloes/32/5926_2.png) [@Marloes](https://community.fibery.io/u/Marloes)\
**Post date:** [January 23, 2024, 8:02am UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/12 "2024-01-23T08:02:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [January 23, 2024, 1:03pm UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/13 "2024-01-23T13:03:20Z")

</div>

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

```auto
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 😉

---

<div class="post-metadata">

**Author:** ![Marloes](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/marloes/32/5926_2.png) [@Marloes](https://community.fibery.io/u/Marloes)\
**Post date:** [January 23, 2024, 9:07pm UTC](https://community.fibery.io/t/script-help-for-templating-a-description-field/5726/14 "2024-01-23T21:07:54Z")

</div>

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