Week 2: Using Calculated Fields for Pre/Post-Fixing Strings

Hi! Sebastian here…

Sometimes I want to prefix or postfix a string before saving it to a record’s field in a project. This could be for a SKU system, a product naming convention, or whatever! Imagine trying to create and store a value like, “SKU-[Item_Classification]-[Item_Number]”, or “[Product_Name] - UNLISTED”.

No matter what it’s for, it usually leads to writing some code on the frontend or backend to handle the concatenation, validation, and any other concerns associated with creating this formatted value.

A really great tip to leverage if you find yourself solving a problem like this is to use a Calculated Field. Instead of having to write extra code in your frontend, or backend, a calculated fields will allow you to implement a MySQL expression that handles it for you - like:

CONCAT(item_category, “-” , item_number)

For example, the screenshot below shows a field called SKU that’s configured to concatenate two other fields on the table into a single formatted string. Pretty cool, right?

Using Calculated Fields for Pre_Post Fixing Strings

There’s a TON more you can do with Calculated Fields. Definitely checkout the 8base Docs here to learn more about them.

2 Likes

Thanks for sharing Sebastian :+1:

Does it also work with 8base auto-generated fields, such as ID ?

Thanks,
Andréas

You certainly can! A calculated field is able to reference any of the managed or custom fields on a record.