# Automate linking between referenced entities

**URL:** <https://community.fibery.io/t/automate-linking-between-referenced-entities/2753>\
**Category:** API & Programming\
**Created:** [April 29, 2022, 6:06am UTC](https://community.fibery.io/t/automate-linking-between-referenced-entities/2753 "2022-04-29T06:06:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Metelerkamp](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/mike_metelerkamp/32/4535_2.png) [@Mike\_Metelerkamp](https://community.fibery.io/u/Mike_Metelerkamp)\
**Post date:** [April 29, 2022, 6:06am UTC](https://community.fibery.io/t/automate-linking-between-referenced-entities/2753/1 "2022-04-29T06:06:34Z")

</div>

Hi, I’d like to find out if anyone might know how to automate entity linking when using references?

e.g. I have a page where we gave conversations and create insights from these conversations on another page called insights by highlighting text and linking to an insight or creating a new one.

When viewing the insight, it shows where it was referenced from but I’d like to add the original entity using relationship field columns.

I’ve looked at automations and the best I can do is link all the conversations, not just the one where it was referenced.

Perhaps there’s a script for filtering out the individual conversation before linking conversations?

---

<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:** [April 29, 2022, 6:33am UTC](https://community.fibery.io/t/automate-linking-between-referenced-entities/2753/2 "2022-04-29T06:33:39Z")

</div>

Is it correctly understood that you would like to auto-link an entity to any other entity that mentions/refers to it?

conversations mention/refer to insights  
insights ↔ conversations (many-to-many relation) to be linked whenever a mention occurs

It is possible using a script in an automation rule, see here:

> [@(How) can I display References in my Board and Feed views?](https://community.fibery.io/t/how-can-i-display-references-in-my-board-and-feed-views/2505/6):
>
> If you create relations between the relevant databases, then something like the following script will populate them with referring entities: const fibery = context.getService('fibery'); 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"]); …

and here:

> [@Make "creation context" available to scripts](https://community.fibery.io/t/make-creation-context-available-to-scripts/2385):
>
> Scenario: I am typing notes in the richtext Description field of a “Meeting Notes” entity. When I create a new Task entity inline (via Ctrl+E), and I want a script to automatically set some fields in the new Task entity upon creation, and it has existing relations that are relevant. But the script needs to know the context in which this new Task entity is created – i.e., this “Meeting Notes” entity is a kind of “parent context” for its creation. The script needs this parent-context to get the…

---

<div class="post-metadata">

**Author:** ![Mike\_Metelerkamp](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/mike_metelerkamp/32/4535_2.png) [@Mike\_Metelerkamp](https://community.fibery.io/u/Mike_Metelerkamp)\
**Post date:** [April 29, 2022, 6:50am UTC](https://community.fibery.io/t/automate-linking-between-referenced-entities/2753/3 "2022-04-29T06:50:20Z")

</div>

Thanks Chris, I’ll try that.

---

<div class="post-metadata">

**Author:** ![Mike\_Metelerkamp](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/mike_metelerkamp/32/4535_2.png) [@Mike\_Metelerkamp](https://community.fibery.io/u/Mike_Metelerkamp)\
**Post date:** [May 4, 2022, 12:20pm UTC](https://community.fibery.io/t/automate-linking-between-referenced-entities/2753/4 "2022-05-04T12:20:21Z")

</div>

@Chr1sG I think “How can I display References in my Board and Feed views?” seems like the closest.

My sandbox board has 3 databases - Customers, Conversations and Insights

We add new customer conversations to ‘conversations’ and have a relationship field linking to the relevant customer.

If any conversation notes are highlighted, we link those to existing/new entities on the insights DB.  
The insights DB has a relation column to conversations and a lookup column for the client(s) linked to the conversation.

Currently the Insights entities only display the list of references and I’d like to automatically populate the relation fields for conversations and clients (with the lookup, this automatically adds a client when a conversation is linked).

I’d like to know how to write the script for the above if possible?

---

<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:** [May 4, 2022, 1:14pm UTC](https://community.fibery.io/t/automate-linking-between-referenced-entities/2753/5 "2022-05-04T13:14:15Z")

</div>

You’ll want an automation (in your Insight DB) that looks something like this:

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

This is the script, but you’ll need to change the SPACE NAME to be correct, at least. Maybe also the database name (_Conversation_ in the example below) and the field name that is used for linking Conversations to an Insight (_Conversations_ in the example below):

```auto
const fibery = context.getService('fibery');
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("SPACE NAME/Conversation", refdoc["FromEntityId"], ["Name"]);

        if (refentity != null) {
            await fibery.addCollectionItem(entity.type, entity.id, "Conversations", refentity['Id']);
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Mike\_Metelerkamp](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/mike_metelerkamp/32/4535_2.png) [@Mike\_Metelerkamp](https://community.fibery.io/u/Mike_Metelerkamp)\
**Post date:** [May 4, 2022, 1:33pm UTC](https://community.fibery.io/t/automate-linking-between-referenced-entities/2753/6 "2022-05-04T13:33:34Z")

</div>

Thanks, I got it to work. Appreciate the help.

---

<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:** [May 4, 2022, 2:06pm UTC](https://community.fibery.io/t/automate-linking-between-referenced-entities/2753/7 "2022-05-04T14:06:00Z")

</div>

Hooray 🎉

---

<div class="post-metadata">

**Author:** ![antoniokov](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/antoniokov/32/201_2.png) [@antoniokov](https://community.fibery.io/u/antoniokov)\
**Post date:** [May 5, 2022, 9:38am UTC](https://community.fibery.io/t/automate-linking-between-referenced-entities/2753/8 "2022-05-05T09:38:17Z")

</div>

I wonder: what are you trying to achieve at the end?

- see relevant Insights when opening a Customer record?
- calculate how many paid Customers are connected to a particular Insight?
- …

Maybe, going one step further: what questions you’d like to answer, what decisions you’d like to make?

We are working on a similar set of use cases right now, and it’d be great to incorporate your scenarios as well.
