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:
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();
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 but feel free (anyone) to figure it out.
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.
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.
@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)