# Link user that is mentioned in a comment

**URL:** <https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042>\
**Category:** Get Help\
**Created:** [August 30, 2023, 2:24pm UTC](https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042 "2023-08-30T14:24:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![YvetteLans](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/yvettelans/32/6101_2.png) [@YvetteLans](https://community.fibery.io/u/YvetteLans)\
**Post date:** [August 30, 2023, 2:24pm UTC](https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042/1 "2023-08-30T14:24:27Z")

</div>

Hi,

What is the solution/script to link a user to a field when the user is mentioned in a comment?

So example:

- When a Fibery user is mentioned in a comment (within a task)
- Then link the user to field ‘Mentioned’

(we have another use case; but this explains it simply)

Thanks!

---

<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:** [August 30, 2023, 3:31pm UTC](https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042/2 "2023-08-30T15:31:37Z")

</div>

You would have to

- create an automation that runs when a comment is added
- use a script action to  
– extract the latest comment  
– trawl the comment for mentions (using regex pattern matching)  
– link the mentioned user(s) to the entity

This assumes that you’re talking about the Comments field.

If you are talking about inline comments, it’s another story

---

<div class="post-metadata">

**Author:** ![YvetteLans](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/yvettelans/32/6101_2.png) [@YvetteLans](https://community.fibery.io/u/YvetteLans)\
**Post date:** [August 30, 2023, 5:07pm UTC](https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042/3 "2023-08-30T17:07:48Z")

</div>

> [@Chr1sG](#):
>
> - use a script action to  
> – extract the latest comment  
> – trawl the comment for mentions (using regex pattern matching)  
> – link the mentioned user(s) to the entity
> 
> This assumes that you’re talking about the Comments field.

Yes, that’s exactly what I need but I’m struggling with the script. Can you share it with me?

Thanks again!

---

<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:** [August 31, 2023, 8:04am UTC](https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042/4 "2023-08-31T08:04:33Z")

</div>

> [@YvetteLans](#):
>
> Yes, that’s exactly what I need but I’m struggling with the script. Can you share it with me?
> 
> Thanks again!

I recall that you already had a script that you were using to detect mentions in comments and create custom notifications, or do I misremember?  
It would seem that this need is the same, but just needs modifying to link the user instead of creating a new entity.

---

<div class="post-metadata">

**Author:** ![YvetteLans](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/yvettelans/32/6101_2.png) [@YvetteLans](https://community.fibery.io/u/YvetteLans)\
**Post date:** [September 1, 2023, 7:05am UTC](https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042/5 "2023-09-01T07:05:09Z")

</div>

Hi Chris,

The script that we had, fetched the mentions from the comments of a task and created a new entity in the separate notification database. That worked perfectly.

In this scenario we need to fetch the mention of the comment within the entity itself and then link the user to the correct field.

I’ve tried to adjust the script to accomplish this. But because the original was quite complex (because of the two separate databases) I need some help.

I’m not a script hero 🙈

---

<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:** [September 1, 2023, 8:55am UTC](https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042/6 "2023-09-01T08:55:20Z")

</div>

> [@YvetteLans](#):
>
> we need to fetch the mention of the comment within the entity itself

I’m not sure what this means. Just to double check, are we talking about inline comments or the comments field?

---

<div class="post-metadata">

**Author:** ![YvetteLans](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/yvettelans/32/6101_2.png) [@YvetteLans](https://community.fibery.io/u/YvetteLans)\
**Post date:** [September 4, 2023, 7:59am UTC](https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042/7 "2023-09-04T07:59:18Z")

</div>

Apologies if I’m not clear!

I’m talking about the comments field.

So example:

- If a user is mentioned in the comment of a task
- Then link the user that is mentioned in the comment of the task to the field ‘involved team mates’

---

<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:** [September 4, 2023, 8:02am UTC](https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042/8 "2023-09-04T08:02:45Z")

</div>

Do you still have the previous automation script for detecting mentions? I can save myself some time if I can re-use some chunks of it 🙂

---

<div class="post-metadata">

**Author:** ![YvetteLans](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/yvettelans/32/6101_2.png) [@YvetteLans](https://community.fibery.io/u/YvetteLans)\
**Post date:** [September 4, 2023, 8:27am UTC](https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042/9 "2023-09-04T08:27:19Z")

</div>

Hi Chris,

After the release of the new Inbox we’ve cleaned up our ‘own notication system’ + automations/scripts 🙈.

So the exact scripts we had then are gone, but when I’ve tried to fix it myself, I’ve used the scripts that you’ve mentioned in [this post](https://community.fibery.io/t/resolved-comments-disappear-bug-or-by-design-our-comments-database-set-up/4610/10). Because I don’t fully understand the script it’s hard for me to adjust it to this use case.

---

<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:** [September 4, 2023, 8:46am UTC](https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042/10 "2023-09-04T08:46:42Z")

</div>

Ah yes, thanks for reminding me.

I think for the use case you describe above, the script needs to look something like this:

```auto
const fibery = context.getService('fibery');

function getMentions(text) {
    const regex = /(?<=\[\[#\@[a-f0-9-]+\/)[a-f0-9-]+/g;
    const found = text.match(regex);
    return found;
}

for (const entity of args.currentEntities) {
    const entityWithComments = await fibery.getEntityById(entity.type, entity.id, ['Comments']);
    const lastComment = entityWithComments['Comments'].pop();
    const content = await fibery.getDocumentContent(lastComment['Document secret'], 'md');
    const matches = getMentions(content);
    if (matches) {
        for (const match of matches) {
            await fibery.addCollectionItem(entity.type, entity.id, 'Mentioned', match);
        }
    }
}

```

You just need to add this to an automation whose trigger is ‘Entity linked to a XXX … Comments’

---

<div class="post-metadata">

**Author:** ![YvetteLans](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/yvettelans/32/6101_2.png) [@YvetteLans](https://community.fibery.io/u/YvetteLans)\
**Post date:** [September 4, 2023, 10:05am UTC](https://community.fibery.io/t/link-user-that-is-mentioned-in-a-comment/5042/11 "2023-09-04T10:05:53Z")

</div>

Awesome! Works great, thank you so much!

Maybe also a nice one to add to Fibery’s guide? It’s a quite common use case I think 😄
