# Using References in automation/scripts

**URL:** <https://community.fibery.io/t/using-references-in-automation-scripts/1836>\
**Category:** Ideas & Features\
**Tags:** automations\
**Created:** [July 25, 2021, 11:01pm UTC](https://community.fibery.io/t/using-references-in-automation-scripts/1836 "2021-07-25T23:01:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cannibalflea](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/cannibalflea/32/318_2.png) [@cannibalflea](https://community.fibery.io/u/cannibalflea)\
**Post date:** [July 25, 2021, 11:01pm UTC](https://community.fibery.io/t/using-references-in-automation-scripts/1836/1 "2021-07-25T23:01:19Z")

</div>

I raised a concern in [another thread about creation of entities in rich text areas](https://community.fibery.io/t/improve-speed-of-creating-entities-in-rich-text/1831):

> [@Improve Speed of Creating Entities in Rich Text](https://community.fibery.io/t/improve-speed-of-creating-entities-in-rich-text/1831/3):
>
> My other frustration is that although project is a relation field for both meeting notes and tasks, when I create a task in a meeting note, the project information doesn’t get populated in the task. I have to manually click each one. I know this is by design and requires careful consideration but it would really make free flow entity creation that much more intuitive.

@Chr1sG kindly provided an automation solution:

> [@Improve Speed of Creating Entities in Rich Text](https://community.fibery.io/t/improve-speed-of-creating-entities-in-rich-text/1831/4):
>
> You can use this automation:
> 
> ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/c/ca2d103e93f85d65fca353ba64eddb57ac4ccb65.png)
> 
> With this, when a task is created from a meeting (and therefore becomes linked to it) it will automatically be linked to the project that the meeting belongs to.

I thought the automation discussion is probably off-topic so I thought I would move the discussion here.

The solution provided above only works if the Task entity has a direct relation to the Meeting entity. In some cases, there isn’t direct relation (e.g. the task and meeting are both related to a Project entity but not to each other) so I was thinking the only way is to use the **References**. However, when I retrieve the References object, it only seems to provide the Ids and not the referencing entity type.

Has anyone ever worked with References objects through the API? Is it possible to actually retrieve the original entity/entities that referred to the current entity and grab some of their attributes? `fibery.getEntityById()` seems to require the type.

---

<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:** [July 26, 2021, 8:44am UTC](https://community.fibery.io/t/using-references-in-automation-scripts/1836/2 "2021-07-26T08:44:11Z")

</div>

> [@cannibalflea](#):
>
> the only way is to use the **References**

or you could add a relation between Meetings and Tasks 😉

Unfortunately, I think you’re right that it is not possible to get the type of an entity just from knowing its Id, so unless you already know the type(s) of the referring entities, it will be hard (or at least long-winded) to figure it out.

Perhaps @Sergey_Truhtanov or someone else would like to chip in (or even add functionality to retrieve an entity’s type from its Id) 🙂

---

<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:** [July 26, 2021, 10:24am UTC](https://community.fibery.io/t/using-references-in-automation-scripts/1836/3 "2021-07-26T10:24:08Z")

</div>

With respect to the issue of linking a Task to the Project that a referring Meeting is owned by, I think you could iterate through the referring entities to find only those which are of the Meeting type, and link to the parent Project when a match is found.  
Of course, it begs the question of what should happen if multiple Meetings (with different Project parents) refer to the same Task. I had assumed that you would only want to link a Task to a single Project.

---

<div class="post-metadata">

**Author:** ![Sergey\_Truhtanov](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/sergey_truhtanov/32/13986_2.png) [@Sergey\_Truhtanov](https://community.fibery.io/u/Sergey_Truhtanov)\
**Post date:** [July 26, 2021, 4:34pm UTC](https://community.fibery.io/t/using-references-in-automation-scripts/1836/4 "2021-07-26T16:34:34Z")

</div>

So you create a Task in a Meeting entity, by selecting some text of a Meeting and converting it to an Entity. And you want those Task to automatically inherit Project of a Meeting.

Unfortunately it is not possible at the moment. We have some direct plans for more intelligent References support in formulas. As we suffer ourselves from not being able to make some further calculations on References of concrete type. Like  
`Refereces.Filter(Type = "Feature").Sum([Some Feature field here])`.  
But cannot make any concrete promise of when this will be implemented, need to find out right solution first.

---

<div class="post-metadata">

**Author:** ![cannibalflea](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/cannibalflea/32/318_2.png) [@cannibalflea](https://community.fibery.io/u/cannibalflea)\
**Post date:** [July 26, 2021, 4:55pm UTC](https://community.fibery.io/t/using-references-in-automation-scripts/1836/5 "2021-07-26T16:55:41Z")

</div>

@Sergey_Truhtanov Is it possible to do more with references through scripts? I was able to retrieve the ID’s of an entity’s references:

```auto
{
  References: [
    { Id: 'f657d33a-a167-4278-873f-24fa46b05805' },
    { Id: '6a3cb715-e8b3-481f-b8f4-00c29da559be' }
  ]
}

```

Is it possible have `fibery.getEntityById()` to return both the ID and Type of each referring entity? Then we can use `fibery.getEntityById()` again to iterate over the references and do some more interesting things.

---

<div class="post-metadata">

**Author:** ![Sergey\_Truhtanov](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/sergey_truhtanov/32/13986_2.png) [@Sergey\_Truhtanov](https://community.fibery.io/u/Sergey_Truhtanov)\
**Post date:** [July 26, 2021, 4:58pm UTC](https://community.fibery.io/t/using-references-in-automation-scripts/1836/6 "2021-07-26T16:58:45Z")

</div>

That will work for sure.

---

<div class="post-metadata">

**Author:** ![Sergey\_Truhtanov](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/sergey_truhtanov/32/13986_2.png) [@Sergey\_Truhtanov](https://community.fibery.io/u/Sergey_Truhtanov)\
**Post date:** [July 26, 2021, 5:01pm UTC](https://community.fibery.io/t/using-references-in-automation-scripts/1836/7 "2021-07-26T17:01:09Z")

</div>

Be a bit careful with that “Created” trigger by the way. As there are many places to create an entity in Fibery, it may appear that some fields will be empty in your script, if for example entity is created from a table or smth like this. If you need to always update some state on field change, I recommend to use “Updated” trigger and selecting field you need to subscribe to. But this is just a general note, may not be applicable to your case.

---

<div class="post-metadata">

**Author:** ![cannibalflea](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/cannibalflea/32/318_2.png) [@cannibalflea](https://community.fibery.io/u/cannibalflea)\
**Post date:** [July 26, 2021, 5:14pm UTC](https://community.fibery.io/t/using-references-in-automation-scripts/1836/8 "2021-07-26T17:14:06Z")

</div>

Thanks for the reminder on this. I will definitely need to incorporate some checks to catch these (or just use “Updated” trigger as suggested).

However, I am wondering how I can iterate over the reference entities with just the IDs. It seems `getEntityByID()` also requires the type:

```auto
fibery.getEntityById(type: string, id: string, fields: string[])

```

---

<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:** [July 26, 2021, 5:35pm UTC](https://community.fibery.io/t/using-references-in-automation-scripts/1836/9 "2021-07-26T17:35:26Z")

</div>

If you have an entity with multiple references. you can check to see which are from Meeting entities using the following:

```auto
for (const entity of args.currentEntities) {
    const entityWithRefs = await fibery.getEntityById(entity.type, entity.id, ["References"]);
    for (const ref of entityWithRefs["References"]) {
        const refdoc = await fibery.getEntityById("Collaboration~Documents/Reference", ref["Id"], ["FromEntityId"]);
        const refentity = await fibery.getEntityById("App_name/Meeting_type_name", refdoc["FromEntityId"], ["Name"]);
        if (refentity != null) {
            console.log(refentity["Name"])
            ...your code here
        }
    }
}

```
