# Insert entity reference with scripts or markdown templates

**URL:** <https://community.fibery.io/t/insert-entity-reference-with-scripts-or-markdown-templates/2976>\
**Category:** Get Help\
**Created:** [June 18, 2022, 9:21pm UTC](https://community.fibery.io/t/insert-entity-reference-with-scripts-or-markdown-templates/2976 "2022-06-18T21:21:00Z")\
**Posts on this page:** 3\
**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:** [June 18, 2022, 9:21pm UTC](https://community.fibery.io/t/insert-entity-reference-with-scripts-or-markdown-templates/2976/1 "2022-06-18T21:21:01Z")

</div>

I am wondering if there is currently a way of inserting entity references in rich text areas via scripts or through markdown templates.

The [help page on markdown templates](https://help.fibery.io/en/articles/5170015-markdown-templates) seems to show only inserting some attributes as simple text. I thought that using inline javascript might help but I couldn’t find a way of constructing an inline reference and appending it to the rich text field.

---

<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:** [June 19, 2022, 8:16am UTC](https://community.fibery.io/t/insert-entity-reference-with-scripts-or-markdown-templates/2976/2 "2022-06-19T08:16:04Z")

</div>

A #mention to an entity is constructed in a Fibery-markdown compatible form as follows:  
[[#^xxxxxx/yyyyyy]]  
where xxxxxx is the ID of the database and yyyyyy is the ID of the entity.

You can use scripting to determine xxxxxx and yyyyyy.

I hope that you already know how to get yyyyyy.

For the database ID, you’ll probably need something like this:

const fibery = context.getService(‘fibery’);  
const schema = await fibery.getSchema();  
const dbID = schema[‘typeObjects’].filter((obj) =\> obj.name == ‘spacename/dbname’)[0][‘id’];

I hope this makes sense, and is informative enough to allow you to experiment and achieve what you want to do.

---

<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, 2022, 4:42am UTC](https://community.fibery.io/t/insert-entity-reference-with-scripts-or-markdown-templates/2976/3 "2022-07-25T04:42:15Z")

</div>

Just in case some else is looking for something similar, here is a how I used markdown to insert a table of outstanding tasks into my “Daily Notes” entries:

```auto
# ✅ Tasks
| **Task** | **Due Date** |
| --- | :-: |<%
    const fibery = context.getService('fibery');
    const APP_NAME = "Tasks";
    const ENTITY_TYPE = "Task";
    const SET_TZ ='America/Seattle';

    function getLocalDate(strDate) {
        const dateUTC = new Date(strDate);
        return dateUTC.toLocaleString('en-US', { timeZone: SET_TZ, weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', hour12: true });
    }

    const queryOpenTasks = `{
        findTasks(orderBy: {daysRemaining: ASC}, daysRemaining: {lessOrEquals: 7}, status: {name: {notIn: ["Done"]}}) {
            id
            name
            dueDate
        }
    }`;
    const myOpenTasks = await fibery.graphql('Tasks', queryOpenTasks);

    const schema = await fibery.getSchema();
    const dbID = schema['typeObjects'].filter((obj) => obj.name == `${APP_NAME}/${ENTITY_TYPE}`)[0]['id'];

    myOpenTasks.data.findTasks.forEach((task) => {%>
| [[#^<%= dbID %>/<%= task.id %>]] | <%= getLocalDate(task.dueDate) %> | <% });%>

```

I must say the new graphql function makes things alot easier (although I am not sure if I’m doing it properly).

Fibery’s markdown table implementation is a bit buggy as it doesn’t respect alignment and table headers.

All of this will be moot once you are able to include live table views as blocks, but thought I should share since @Chr1sG shared the main secret 😁
