Make "creation context" available to scripts

Scenario: I am typing notes in the richtext Description field of a “Meeting Notes” entity.

When I create a new Task entity inline (via Ctrl+E), and I want a script to automatically set some fields in the new Task entity upon creation, and it has existing relations that are relevant.

But the script needs to know the context in which this new Task entity is created – i.e., this “Meeting Notes” entity is a kind of “parent context” for its creation.

The script needs this parent-context to get the related “Project” for the new Task entity (e.g. the Project relation in the “Meeting Notes” entity).

I don’t know how useful it is for your scenario, but it is possible to find stuff out about the items that are referring to an entity.

There is a (hidden) field called ‘References’ which is a collection of entities of the ‘Collaboration~Documents/Reference’ type. Each Reference entity contains all the useful info about the reference, including the ID of the referring entity.

Here’s an example of how you can access the ID of the first reference in the collection and thereby get the ID of the referring entity:

const entityWithExtraFields = await fibery.getEntityById(entity.type, entity.id, ['References']);
const firstRefID = entityWithExtraFields['References'][0]['Id']
const refWithDetails = await fibery.getEntityById('Collaboration~Documents/Reference', firstRefID, ['FromEntityId'])

You can probably figure out some of the rest yourself, but to give you a steer in the right direction, here’s a idea of the fields that the ‘Collaboration~Documents/Reference’ type contains:

image

I could imagine for your use case, you would want to find the most recently created reference, then find the FromEntityID…

Feel free to let us know how you get on :slight_smile:

@Chr1sG, yes that’s useful for the example I described above, where we know there will be a reference in a specific field.

But in other cases this would not work; i.e. if we are creating an entity within a table view’s relation cell. In this case a script could still benefit from knowing the context/parent for the object creation. Or would such a reference field be automatically set?

This actually does not work for my current use case.

The scenario is: I am taking notes in a Meeting Notes entity’s Description field, and I create a new Task entity inline in via Ctrl+E.

The goal is to have a script set the this new Task entity’s Project relation, from a field in the Meeting Notes entity (which is what I’ve been calling the its “creation context”).

So I made a Task Rule that runs upon its creation: a small script reads its References array, where I expected to find a single reference to the Meeting Notes entity from where I just created this new TaskBut the References array is empty.

Apparently the Reference is not added until after the “Created” Rule runs, so I see no way to do this currently. :cry:

So, I’m back to my original request that an “entity created” Rule can access its “creation context”.

Yes, unfortunately, references are not immediately present, due to how they are implemented.
I know it’s a workaround, but you could create a formula field which counts the number of references to the Task, and use a change in this value to trigger the script.

See below…

1 Like

I may be wrong regarding what you are trying to achieve, but I think you can already do this with automations. I had the exact same need and was tired of having to click through and add these details.

In my setup, I have Project Meeting entity/database type and a Task type. In the Task type I have the following automation rule:

The rule then triggers the following script:

// Script parameters
const PARENT_TYPE = 'Project Management/Project';
const REF_ENTITY_TYPES = ['Project Management/Project Meeting', 'Project Management/Project Review', 'Project Management/Project Update'];
const REF_ENTITY_LINK_FIELD = 'Project';
const ENTITY_LINK_FIELD = 'Project';

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

// Iterate through the entities
for (const entity of args.currentEntities) {
    // Set the referring field if empty
    if (entity[ENTITY_LINK_FIELD].id == null) {
        const entityWithRefs = await fibery.getEntityById(entity.type, entity.id, ['References']);

        // iterate through the references and update with first reference to a project meeting
        for (const ref of entityWithRefs['References']) {
            const refDoc = await fibery.getEntityById('Collaboration~Documents/Reference', ref['Id'], ['FromEntityId', 'FromEntityType']);
            const refType = refDoc['FromEntityType'].name;

            if (refType == PARENT_TYPE) {
                // The referring entity is the actual PARENT_TYPE (so id can be used directly)
                const refEntity = await fibery.getEntityById(refType, refDoc['FromEntityId'], ['Name']);
                await fibery.updateEntity(entity.type, entity.id, { [ENTITY_LINK_FIELD]: refEntity.id });
                break;
            } else if (REF_ENTITY_TYPES.includes(refType)) {
                // Referring entity has a relation to the PARENT_TYPE
                const refEntity = await fibery.getEntityById(refType, refDoc['FromEntityId'], ['Name', REF_ENTITY_LINK_FIELD]);
                if (refEntity != null && refEntity[REF_ENTITY_LINK_FIELD].id != null) {
                    await fibery.updateEntity(entity.type, entity.id, { [ENTITY_LINK_FIELD]: refEntity[REF_ENTITY_LINK_FIELD].id });
                    break;
                }
            }
        }
    }
}

With this setup I am actually to establish links from a few different databases that have a relation to a project (Project Meeting, Project Review and Project Update) as well as from the actual Project entities themselves.

I don’t know if this is helpful.

4 Likes

Indeed, my workaround (adding a ref count formula and using it to trigger) is no longer necessary :slight_smile:

@cannibalflea - what triggers your script? I am hoping for a solution that operates when the entity is created (e.g. via Ctrl+L in a Rich Text field), before the user has manually set up any fields.

1 Like

@Matt_Blais the script is basically triggered a few seconds after the task is created in a Project Meeting/Project Review/etc. entity’s rich text area. When I mean created inline, I mean either through the # tag or through ctrl+E. As you noted, there is a lag between the task entity’s creation and the reference collection being populated.

I also apply a filter on the Project field so that it won’t run after the project field has been initially set (based on assumption that when created inline, the first reference is the determining one).

Just wanted to add that having this work around doesn’t negate the underlying request that there might be value to the underlying contextual information for certain workflows. This is particularly important if/when Fibery starts allowing plug-ins and extensions from developers.

2 Likes