# Button to create entity in different space

**URL:** <https://community.fibery.io/t/button-to-create-entity-in-different-space/4561>\
**Category:** Get Help\
**Created:** [June 7, 2023, 9:14am UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561 "2023-06-07T09:14:06Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![fnagy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/fnagy/32/6813_2.png) [@fnagy](https://community.fibery.io/u/fnagy)\
**Post date:** [June 7, 2023, 9:14am UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/1 "2023-06-07T09:14:06Z")

</div>

Hello,

I have two spaces, each with one database: DB1 with Action and DB2 with Task.

I want to create a button in Action so that it creates a Task with a pre populated description like “created from Action#123”.

Is this possible to achieve with a script? Can you point me in the right direction?

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:** [June 7, 2023, 9:24am UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/2 "2023-06-07T09:24:11Z")

</div>

Are the databases related? If not, then the only way is with a script.  
If they are related, you should see an ‘Add Task’ option in the Action button. Then you can use a formula to set the name of the Task.  
Something like `[Step 1 Action].Name` should work.

---

<div class="post-metadata">

**Author:** ![fnagy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/fnagy/32/6813_2.png) [@fnagy](https://community.fibery.io/u/fnagy)\
**Post date:** [June 7, 2023, 9:36am UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/3 "2023-06-07T09:36:12Z")

</div>

No they are not related, so yes I was looking at creating a script but it is my first time creating one and I have never worked with Javascript.

Will this guide help me achieve what I want? [. | Fibery](https://the.fibery.io/@public/User_Guide/Guide/Scripts-54)

---

<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 7, 2023, 9:51am UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/4 "2023-06-07T09:51:12Z")

</div>

Yes, that guide is where to start if you want to script stuff, but I’d also be curious as to whether it might make sense to create a relationship between the dbs, if only to make this automation easier.  
You can even hide the relation in views where you don’t need it.

---

<div class="post-metadata">

**Author:** ![fnagy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/fnagy/32/6813_2.png) [@fnagy](https://community.fibery.io/u/fnagy)\
**Post date:** [June 7, 2023, 2:29pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/5 "2023-06-07T14:29:15Z")

</div>

I would rather have not the relation in, as it keeps the workspace map cleaner and also it is a different type of relation compared to all other relations (but maybe i am overthinking it lol).

I will test something soon and update here!

---

<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 7, 2023, 2:48pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/6 "2023-06-07T14:48:42Z")

</div>

Well, if you want to do it via scripting, it would look something like this:

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

(you need to provide the correct SpaceName, or it can be omitted if there is only one database called Task in the whole workspace)

---

<div class="post-metadata">

**Author:** ![fnagy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/fnagy/32/6813_2.png) [@fnagy](https://community.fibery.io/u/fnagy)\
**Post date:** [June 8, 2023, 12:50pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/7 "2023-06-08T12:50:31Z")

</div>

Ok your part of the script works (obv :D) - now i am trying to update the description of the task but it says the setDocumentContent is not defined. What am i doing wrong?

```auto
// Developer reference is at api.fibery.io/#action-buttons

// Fibery API is used to retrieve and update entities
const fibery = context.getService('fibery');

// affected entities are stored in args.currentEntities;
// to support batch actions they always come in an array
for (const entity of args.currentEntities) {
    // an entity contains all fields apart from collections;
    // to access a field refer to it by its UI name

    const thenewtask = await fibery.createEntity('V2 Product Management/Task', { 'Name': entity['name'] });
     console.log(args.currentEntities['0']);
     console.log(thenewtask);
     const descrid = thenewtask['description']//.map(({ secret }) => secret);
    console.log(descrid.secret)
     await setDocumentContent('descrid.secret', 'teststring');
    //await fibery.createEntity('V2 Product Management/Task', {'Description':"test"});
     
   // await fibery.updateEntity(entity.type, entity.id, {
        // 'Field Name': newValue
   // });
}

```

---

<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 8, 2023, 2:35pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/8 "2023-06-08T14:35:54Z")

</div>

Couple of things: you need to set the service `fibery` for the `setDocumentContent` to work, and you need to remove the quotes from `descrid.secret`

Try this  
`await fibery.setDocumentContent(descrid.secret, 'teststring');`

---

<div class="post-metadata">

**Author:** ![fnagy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/fnagy/32/6813_2.png) [@fnagy](https://community.fibery.io/u/fnagy)\
**Post date:** [June 8, 2023, 2:47pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/9 "2023-06-08T14:47:03Z")

</div>

Yes that works! Apologies if it was too obvious; this is my first time writing Javascript and i clearly overlooked at the basics

---

<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 8, 2023, 3:01pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/10 "2023-06-08T15:01:25Z")

</div>

No apology needed - I’m impressed that you’re giving it a go. Most people would not even bother trying.

---

<div class="post-metadata">

**Author:** ![dukevannori](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@dukevannori](https://community.fibery.io/u/dukevannori)\
**Post date:** [August 16, 2023, 3:11pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/11 "2023-08-16T15:11:39Z")

</div>

Hi - trying to do this with related DBs:

i integrated your email template space to import emails from gmail (awesome btw!!)

now i have CRM Space and have set up the relation to automatically link contacts by email.  
but sometimes a contact has not been setup - and I want to have a button to create a new contact with a click.

 ![Screenshot 2023-08-16 at 17.12.04](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/b/b7bdc6d338089e1201beba0356fc1901cc1d68ba.png)

in the dropdown the option does not show up to create an entity in the CRM Space … only update

 ![Screenshot 2023-08-16 at 17.12.47](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/8/84dba4c25b2ffbb98aa16522a077a52332368596.png)

do i need to setup another relationship?

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 16, 2023, 3:16pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/12 "2023-08-16T15:16:02Z")

</div>

It’s not possible to use an automation action (button or rule) create an entity in a linked database if the relation is an auto-relation.

You could create a dummy (normal) relation solely for this purpose though, and then just hide it everywhere on the UI.

---

<div class="post-metadata">

**Author:** ![dukevannori](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@dukevannori](https://community.fibery.io/u/dukevannori)\
**Post date:** [August 16, 2023, 3:23pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/13 "2023-08-16T15:23:15Z")

</div>

i see - so script it is 🙂

---

<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 16, 2023, 3:24pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/14 "2023-08-16T15:24:53Z")

</div>

> [@dukevannori](#):
>
> i see - so script it is 🙂

![200w](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/7/7a03f20b2d7d659770b2cb1f7946ff99c6d9fbd3.gif)

---

<div class="post-metadata">

**Author:** ![Robert\_B](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/robert_b/32/7198_2.png) [@Robert\_B](https://community.fibery.io/u/Robert_B)\
**Post date:** [September 15, 2023, 8:26pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/15 "2023-09-15T20:26:38Z")

</div>

Hi Chris! Can you explain this a little more?

If I have a couple databases that have a many-to-many relation, can I used an automation (button or rule)?

Is it just the “automatically link” checkbox kind that doesn’t work?

I think you are referring to that “automatically link” checkbox.

By the way, I can’t seem to wrap my head around what the “automatically link” checkbox does in general. I’ve read the Fibery materials and still don’t understand it (my issue not Fibery’s) - could you explain it in a very basic manner?

Thank you and have a great weekend!

---

<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 15, 2023, 9:15pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/16 "2023-09-15T21:15:47Z")

</div>

An auto-relation can be thought of as a normal relation, except that Fibery takes care of linking items based on them matching on specific criteria.

So for example, you could have a database of employees and auto-link it (one-to-one) to the user database, based on a match between the email field in each db.  
That way, for each employee you would see a link to their user account (if they have one - perhaps some employees are not users) and for each user you can see a link to the corresponding employee entity (if there is one - perhaps some users are not employees).

The db fields created by an auto-relation (one at each end of the relation) are read-only. Users can’t link/unlink entities - the whole point is that Fibery links entities when there are matches.

This means that some of the automation actions usually available for relation fields, e.g link/unlink/add/delete are not available.

In your case, if you (only) have an auto-linking relation between the gmail contact db and the ‘normal’ contact db, you can’t use an automation in the google contact db to create a ‘normal’ contact since this action will not be available.

So your choice is to create a dummy relation, whose sole purpose is to provide this capability (and still rely on the auto-linking to connect entities where possible) or to write a script, since a script can do stuff that isn’t possible with standard no-code actions.

---

<div class="post-metadata">

**Author:** ![Robert\_B](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/robert_b/32/7198_2.png) [@Robert\_B](https://community.fibery.io/u/Robert_B)\
**Post date:** [September 16, 2023, 12:31pm UTC](https://community.fibery.io/t/button-to-create-entity-in-different-space/4561/17 "2023-09-16T12:31:17Z")

</div>

Thanks Chris, that’s super helpful.
