Basic script for 'link existing entity or create a new one'


We have a lot of automations that goes like this:

  • Check if an entity with [value] already exists in Database [ x ]
  • If so, then update field [1] with the found entity (or when it’s a many relation; link found entity)
  • If not, then create a new entity in Database [ x ] and update field [1] with the entity you’ve created (or when it’s a many relation; link found entity)

Because we can’t do that via normal automations in Fibery we have all kind of clumpsy workarounds.

  • First we check if we can link an entity
  • If not, then we set a checkbox ‘create new entity’ to true
  • That triggers another automation which creates the new entity

The problem however, is that we have a lot of automations and data to process. We’re experiencing queues/delays in the workspace that also ruin other automations for users.

Long story short:

Does anybody knows a ‘basic script’ for this purpose that can be understood by a person that is not great in scripting (aka: me :nerd_face:).

So that we only need 1 automations instead of 2 (and sometimes even more if we also check on things like existing contact, existing product, existing event type etc.)


1 Like

Give me some examples of the relevant space/db/field names, plus the query to be run which will determine if an entity already exists, and I’ll try and post a script example (for to-one or for to-many relations).

Thank you so much!

I’ve created a simplified test set-up that covers most of the scenarios.

It has 3 different spaces.

  • Space product
    • Database: Product
  • Space order
    • Database: Order
  • Space contact
    • Database: Contact
    • Database: Contact type (Lead, Customer or Partner)
    • Database: Email address

Trigger of the automation

  • A new order is created in Space order/Order

Needed automation:

  1. Check if you can find an existing contact with the email address of the order. You can either search for the email address entity or in the SYSTEM - emails field (which is a join formula)

  1. If you find an existing contact, then link that contact to the order and link the contact type ‘Customer’ to the contact.


  1. If you can’t find an existing contact, then create a new contact (Name = First name + Last name from the order) and a new Email address entity (Name = email from the order). Link the email address to the contact. Link the contact type ‘Customer’ to the contact. And link the contact to the order.

  2. Check if you can find the Product name in the order in the Space product/Product database. If so, link the found product to the order. If not, create a new product (Name = product name) and link that product to the order.


Feel free to use the set-up in the test space.

Thanks again for the help. Maybe an idea to add this example use case in the Fibery Guide as well?

PS. in our own set-up we also have a relation between Space Order/Order database and Space Contact/Email address database. When you use normal automations that relation is needed but I hope it’s not necessary via script. Else; feel free to add the relation.

1 Like

I gather that Contact and Email address dbs have a one-to-many relation, so wouldn’t it be OK to search for an email in the Email address db and, if found, link the Contact that the Email address belongs to?

Will there only ever be one Product listed in the Product name text field? Can an order be linked to multiple Products via the relation field? Your test space suggests it can, but I don’t understand why…

Here’s an example of a script that does pretty much what you need. I assumed that an Order can only link to one product, since this seemed logical to me (and your example test space seems to contain a contradiction where an Order can have a single Product name but can link to multiple Products).

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

for (const order of args.currentEntities) {
    // find the id for the 'Customer' Contact type
    let response = await fibery.graphql('Space contact', 'query findCustomerType{findContactTypes(name:{is:"Customer"}){id}}');
    const customerTypeId = response['data']['findContactTypes'][0]['id'];


    let customerId = '';

    // look for any matching Email address and thereby Contact
    response = await fibery.graphql('Space contact', 'query findContactFromEmail($searchEmail:String){findEmailAddresses(name:{is:$searchEmail}){id,contact{id}}}', { 'searchEmail': order['Email address'] });

    if (response['data']['findEmailAddresses'].length > 0) {
        // get the Contact id of the first matching Email address
        customerId = response['data']['findEmailAddresses'][0]['contact']['id'];
    else {
        // create new Contact
        const newCustomer = await fibery.createEntity('Space contact/Contact', { 'name': order['First name'] + " " + order['Last name'] });
        customerId = newCustomer['id'];

        // create new Email address and link to Contact
        await fibery.createEntity('Space contact/Email address', { 'name': order['Email address'], 'Contact': customerId });

    // add 'Customer' as contact type
    await fibery.addCollectionItem('Space contact/Contact', customerId, 'Contact types', customerTypeId);

    // add customer Contact to Order
    await fibery.updateEntity(order.type,, { 'Contact': customerId });


    let productId = '';

    // look for any matching Product
    response = await fibery.graphql('Space product', 'query findProduct($searchProduct:String){findProducts(name:{is:$searchProduct}){id}}', { 'searchProduct': order['Product name'] });

    if (response['data']['findProducts'].length > 0) {
        // get the id of the first matching Product
        productId = response['data']['findProducts'][0]['id'];
    else {
        // create new Product
        const newProduct = await fibery.createEntity('Space product/Product', { 'name': order['Product name'] });
        productId = newProduct['id'];

    // add Product to Order
    await fibery.updateEntity(order.type,, { 'Product': productId });

I hope the general concepts exemplified in the script will help you understand how you can extend this idea to achieve whatever you want.


Thank you so much! This is really helpful to understand the basic principles of scripting in Fibery. Will give it a try in our own workspace :smile:

In reality our orders have multiple products, set by a user or set by the linked order lines (one order line per product). But for this use case it’s not logic indeed!