Autoincrement in calculated field

My project is an Inventory management system. This requires me to have a SKU for each product. Currently our sku numbering system is as follows: “Re_000000”. “Re_” is the prefix and the numbering is autoincrementing. As I can’t do autoincrementing on text I thought doing two fields. One a number field with autoincrementing and the other as calculated text that uses the following formula “CONCAT(“Re_”, skuno)”

Unfortunately when I use this formula I get an error saying that I can’t use autoincrementing fields in calculated fields.

Need help to get this going or a possible work around to get this system up and running.

Thanks,

Hey @keviruchis

If your system is always relying on an auto-incrementing number and then prefixing an “Re_” string, do you actually need to prefix that string in the database? I’d just show “Re_0000020” in the front-end, knowing the SKU value is saved as 20 in the database.

Otherwise, make an auto-incrementing number field in 8base and then write a trigger.after function that runs on Product.create. You could then do something like:

export default await function (event, ctx) {
   const { id, autoIncrementValue } = event.data;
   const newSKU = `RE_${autoIncrementValue}`;

   await ctx.api.gqlRequest(UPDATE_PRODUCT_SKU, { id, newSKU }, { checkPermissions: false })
   
   return {
     data: event.data
   }
}