Button to create entity in different space

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!

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.

1 Like

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

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.

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!

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

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

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?

// 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
   // });
}


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');

2 Likes

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

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

1 Like

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.

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

do i need to setup another relationship?

thanks!

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.

i see - so script it is :slight_smile:

200w

1 Like

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!

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.

1 Like

Thanks Chris, that’s super helpful.

1 Like