Using References in automation/scripts

I raised a concern in another thread about creation of entities in rich text areas:

@Chr1sG kindly provided an automation solution:

I thought the automation discussion is probably off-topic so I thought I would move the discussion here.

The solution provided above only works if the Task entity has a direct relation to the Meeting entity. In some cases, there isn’t direct relation (e.g. the task and meeting are both related to a Project entity but not to each other) so I was thinking the only way is to use the References. However, when I retrieve the References object, it only seems to provide the Ids and not the referencing entity type.

Has anyone ever worked with References objects through the API? Is it possible to actually retrieve the original entity/entities that referred to the current entity and grab some of their attributes? fibery.getEntityById() seems to require the type.

or you could add a relation between Meetings and Tasks :wink:

Unfortunately, I think you’re right that it is not possible to get the type of an entity just from knowing its Id, so unless you already know the type(s) of the referring entities, it will be hard (or at least long-winded) to figure it out.

Perhaps @Sergey_Truhtanov or someone else would like to chip in (or even add functionality to retrieve an entity’s type from its Id) :slight_smile:

With respect to the issue of linking a Task to the Project that a referring Meeting is owned by, I think you could iterate through the referring entities to find only those which are of the Meeting type, and link to the parent Project when a match is found.
Of course, it begs the question of what should happen if multiple Meetings (with different Project parents) refer to the same Task. I had assumed that you would only want to link a Task to a single Project.

So you create a Task in a Meeting entity, by selecting some text of a Meeting and converting it to an Entity. And you want those Task to automatically inherit Project of a Meeting.

Unfortunately it is not possible at the moment. We have some direct plans for more intelligent References support in formulas. As we suffer ourselves from not being able to make some further calculations on References of concrete type. Like
Refereces.Filter(Type = "Feature").Sum([Some Feature field here]).
But cannot make any concrete promise of when this will be implemented, need to find out right solution first.

3 Likes

@Sergey_Truhtanov Is it possible to do more with references through scripts? I was able to retrieve the ID’s of an entity’s references:

{
  References: [
    { Id: 'f657d33a-a167-4278-873f-24fa46b05805' },
    { Id: '6a3cb715-e8b3-481f-b8f4-00c29da559be' }
  ]
}

Is it possible have fibery.getEntityById() to return both the ID and Type of each referring entity? Then we can use fibery.getEntityById() again to iterate over the references and do some more interesting things.

That will work for sure.

Be a bit careful with that “Created” trigger by the way. As there are many places to create an entity in Fibery, it may appear that some fields will be empty in your script, if for example entity is created from a table or smth like this. If you need to always update some state on field change, I recommend to use “Updated” trigger and selecting field you need to subscribe to. But this is just a general note, may not be applicable to your case.

Thanks for the reminder on this. I will definitely need to incorporate some checks to catch these (or just use “Updated” trigger as suggested).

However, I am wondering how I can iterate over the reference entities with just the IDs. It seems getEntityByID() also requires the type:

fibery.getEntityById(type: string, id: string, fields: string[])

If you have an entity with multiple references. you can check to see which are from Meeting entities using the following:

for (const entity of args.currentEntities) {
    const entityWithRefs = await fibery.getEntityById(entity.type, entity.id, ["References"]);
    for (const ref of entityWithRefs["References"]) {
        const refdoc = await fibery.getEntityById("Collaboration~Documents/Reference", ref["Id"], ["FromEntityId"]);
        const refentity = await fibery.getEntityById("App_name/Meeting_type_name", refdoc["FromEntityId"], ["Name"]);
        if (refentity != null) {
            console.log(refentity["Name"])
            ...your code here
        }
    }
}
2 Likes