Automatic Prefix/Emoji before Name when field changes

See the following recommended solution using only a formula, instead of a script:


However, if you like to learn scripting:

This script will prepend any text to the entity Name, separated by two spaces.
The namings of databases and fields can be easily changes in the script at the top (the const variables).

  • Make sure that the related database of the field that triggers the automation (such as a database ‘Type’ or ‘Status’ or anything that you want to show in the Name) has a simple text field ‘Prefix’.

In the automation:

  • IF: Entity is created or field is updated: Field ‘Type’.
  • THEN: add the following script, adjusting the constants names at the top:

chrome_U5tAgQRbbX

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

// Constants for database and field names
const MAIN_DATABASE = 'YourSpaceName/YourCurrentDatabaseName';
const RELATED_DATABASE = 'YourSpaceName/TheRelatedDatabaseName';
const MAIN_NAME_FIELD = 'Name';
const MAIN_RELATED_FIELD = 'Type';
const RELATED_PREFIX_FIELD = 'Prefix';

// Constant for the text pattern to replace (double space in this case)
const TEXT_PATTERN = '  ';

async function processDatabaseRecord() {
    try {
        const currentRecord = args.currentEntities[0];
        const mainRecord = await fibery.getEntityById(MAIN_DATABASE, currentRecord.id, [MAIN_NAME_FIELD, MAIN_RELATED_FIELD]);

        // Remove text to the left of the text pattern, including the pattern itself
        const originalName = mainRecord[MAIN_NAME_FIELD];
        const updatedName = originalName.includes(TEXT_PATTERN) ? originalName.split(TEXT_PATTERN).pop() : originalName;

        let finalName = updatedName;

        // Fetch the related record to get the Prefix
        if (mainRecord[MAIN_RELATED_FIELD]) {
            const relatedRecord = await fibery.getEntityById(RELATED_DATABASE, mainRecord[MAIN_RELATED_FIELD].id, [RELATED_PREFIX_FIELD]);
            const prefix = relatedRecord[RELATED_PREFIX_FIELD] || '';

            // Prepend the Prefix to the updated Name with the text pattern in between if Prefix is not empty
            if (prefix) {
                finalName = `${prefix}${TEXT_PATTERN}${updatedName}`;
            }
        } else {
            console.log(`Related field is empty for database record ID: ${currentRecord.id}.`);
        }

        // Update the main record with the final Name
        await fibery.updateEntity(MAIN_DATABASE, currentRecord.id, { [MAIN_NAME_FIELD]: finalName });

        console.log(`Database record updated. Record ID: ${currentRecord.id}, Final Name: ${finalName}`);
    } catch (error) {
        console.error('Error in script:', error);
    }
}

await processDatabaseRecord();

1 Like

Out of interest, why did you choose to script this?
It looks like it could be done with normal automation actions (using a formula).

Yes, you are probably right, but I tried it and did not manage to make it happen with a formula, and did not want to bother you :wink: but feel free (anyone) to figure it out.

Can I ask where I can customize emoji for each related prefix field ?

The following automation

results in this behaviour
firefox_YuGF9bu9Wj

@Chr1sG your formula:

[Step 1 Leaf].Type.Prefix + " " + ReplaceRegex([Step 1 Page].Name,"^.* ","")

has the following test results:

  • Hello > switch to :ghost: > :ghost: Hello (good)

  • :ghost: Hello > switch to :grinning: > :grinning: Hello (good)

  • :grinning: Hello World > switch to :ghost: > :ghost: World (not good)

  • Hello World > switch to :grinning: > :grinning: World (not good)

  • How are you > switch to :grinning: > :grinning: you (not good)

  • Hello how are you > switch to :ghost: > :ghost: you (not good)

  • [empty] > switch to :grinning: > :grinning:[space] (good)

  • [empty] > switch to [empty prefix] > [space] (not good)

You’ve probably reproduced the formula incorrectly. There are two double spaces in it (replaced with underscores below for clarity)

[Step 1 Page].Type.Prefix + "__" + ReplaceRegex([Step 1 Page].Name, "^.*__", "")

Yes, you are correct and I overlooked that. It indeed works, thank you.

Just one edge case:

[empty name] > switch to [empty prefix] > [double space]

  1. if I create a new Page, and the Type is not automatically set thus empty, the Name becomes two spaces.
  2. if I create a new Page, and the Type does not have a Prefix, the Name becomes two spaces.

When typing in a name, the cursor is most of the time after these spaces, which then need to be removed manually.

You can add an if statement to the formula to only add the double-space if the Type’s Prefix field isn’t empty:

[Step 1 Page].Type.Prefix + If(IsEmpty([Step 1 Page].Type.Prefix), "", " ") + ReplaceRegex([Step 1 Page].Name, "^.* ", "")

(I think discourse is converting the double-spaces into single spaces in the above formula)

1 Like

Congrats, this works perfectly :medal_military:. I’ll add a link to the post itself.

2 Likes

Can you post the formula?

[Step 1 Page].Type.Prefix +
  If(IsEmpty([Step 1 Page].Type.Prefix), "", "  ") +
  ReplaceRegex([Step 1 Page].Name, "^.*  ", "")
1 Like

I dont know why i cant select change fields {Category} in automation



Becuase Debt has a many to many relationship to Category.
For the functionality of adding a Prefix as shown in this topic, you need a many to one relationship.

However, you can still make it work by using the trigger ‘When entity linked’ (Category) and then the linked Category its prefix can be used. Note that you will then need to adapt the formula.


I dont know why the formula cant be trigger

Even I tried lookup field, it’s not work

The formula won’t work for a many-to-many relation.
Also, you can’t use an Emoji (icon) field, you need to use a text field

So I need manually create 1 more text field with the same icon with emoji field ? Do we have any way to convert emoji to text in formula ?

If you want to be able to prepend multiple emojis, then you need to use this formula instead:

[Step 1 Page].Types.Join(Prefix, "") +
  If(Length(Types.Join(Prefix,"")) = 0, "", "  ") +
  ReplaceRegex([Step 1 Page].Name, "^.*  ", "")

and you’ll need two automations (for entity linked and for entity unlinked).

And yes, the linked db needs to have a text field for storing the emoji. In the above example, the relation is called Types, and the Type db has a field called Prefix where the emoji is stored.

1 Like

@Minh_Tri_Do_Hang Using multiple icons is actually a nice idea because then not only entity type (page, idea, task) but also entity status (draft/final/public) and senstitivity (very high, high) and priority (urgent, high, low), department, etc. can be added to a Name, which is very useful in simple views like mobile views.

Another approach is to turn the Name into a formula field, which aggregates several fields. But this requires an extra Title text field also (which needs to be easily accessible for editors)

So if I understand correctly, we only can adjust emoji in name field like this
:fire: :hamburger: {name ⇄ titlte}. do we have way to adjust based on emoji field ?

1 Like