Get all entities in relation

I am trying to create a button that change the entity name of current entity by combining iteration number (iterating over related entities, i.e. index number) and a value from current field—either by getting an array and comparing ID/Name, or a simple index property. Using the actual task ID is a tad too unreadable.

Goal:

  • Feature entity, with many Task entities
  • Task entity, with one Feature
  • In the Task, click on button that rename its own Name field

Example:
Many tasks belong to a “Picture wall” feature, and I want to be able to click the button on one task and it will change name to the current tasks index number + text from one of its other fields.

In my “Tasks” table the name of the task is completely irrelevant, but I still need to make use of it for references (mentions) to it in the details field of other tasks. There’s not much to say about the task that makes it unique, so we’ll end up being confused about which task we are referencing.

Therefore, clicking a button that generates a compound name of sorts, that also includes a unique number as well, for a name like: [Feature name property] [index] [specific property in task].

I’m adept at JavaScript in general, but I don’t quite get the inner workings of the API available for Buttons, and there’s not much documentation to go on yet. My main issue right now is how to get an array of related entities so I can use it to get appropriate index number.

Hi Martin,

The graceful way to do this in the long-term is to generate Task names with a formula. This way the user doesn’t have to click anything to make it work.

However, you cannot set the name using formula (yet!). So let’s create a workaround using an action button. Here is what we’ve got:

The code:

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

for (const entity of args.currentEntities) {
    const feature = await fibery.getEntityById('Feature', entity['Feature'].Id, ['Tasks']);
    const index = feature['Tasks'].findIndex(t => t.id === entity.id) + 1;

    await fibery.updateEntity(entity.type, entity.id, {
        'Name': `${entity['Feature'].Name} ${index} ${entity['Identifier']}`
    });
}

Please let me know if you need any help in adapting this code to your App.

I also recommend taking a look at the action button example in our public repository: fibery-community / Action Buttons · GitLab. The most similar button closes all subtask of a task.

:v:

Thank you very much Anton!

I was able to implement it successfully for my usecase.

By combining a Formula field that simply outputs the name of the entity, I was able to make the name in my table read-only by hiding ‘Name’ and instead showing my formula as the name field.
Now I don’t have to worry much about policing naming conventions :slight_smile:

2 Likes