Business Central Enums and How to Use Them

It has been a few years since the last we saw C/Side included with Business Central new releases. A lot has changed in this time – Microsoft finally fixed their txt2al converter so it would transfer documentation. They have removed the requirement to add an application area to every field or page, added a ton of new events, and most importantly, in the latest versions, they added ENUMS as a replacement for OptionMembers. That is what we will talk about in this article.

 

What Business Central enum is?

Let’s start with what exactly the Business Central ENUM is. Those who come from an object-oriented programming background might already know what the enums are. enums are objects that are essentially a read-only list of items. Another change with the Business Central is that lists begin with the number 0. It goes for Business Central enums as well, so you can refer to specific items both by name and number. If you want to choose the first element, then type in 0 or the element’s name.

 

Business Central enums are declared just like any other object (enum <ID> <Enum name>). You can set whether this enum will be available for extension or not. After you declare enum, you can access it as many times as you need – enum will replace OptionMembers.

To use a Business Central enum, declare a field in a table and set its type to ‘Enum’ instead of ‘Option’. After that, type in enum’s name.

 

You can also use Business Central Enum as variables in the code. You declare it like any other variable, just type in the name of the variable, declare the type of the variable as an enum, and then the name of the enum.

 

After that, you can use enum in code or use the field on the page.

 

Why Business Central enum is outstanding?

This is all good and interesting but is that a reason enough for Microsoft to implement this whole new functionality? Well, no, but there is something outstanding with an enum. Let’s check out what it is and take one of the standard enums that Microsoft implemented.

 

With extensions and AL, we lost any possibility to modify standard option fields. The only way to do something was to recreate the field from the bottom up. In this case, you have to look for any place where the field is used in the code and recreate it. Take note of those values – they are standard values provided by Microsoft. Now, watch the magic happen.

 

WHABAM, what is that!!! A NEW VALUE!!!

 

Business Central enums are extendable

Yes, ladies and gentlemen, Microsoft finally fixed one of the many problems they created with extensions. Business Central enums are special since you can extend them. So far, you can’t delete values from enums, but at least you can add them. So how does this magic happens? Well, let’s take a look.

 

It is not so hard when you know the secret. It’s just like any other extension object: ‘enumextension <ID> <Enum name> extends <Name of standard enum>’. After that, you can create new values for properties. There is just one useful property for enum value, which is called ‘Caption’.

Coming back to custom enums, if you are greedy, you can set your enums not to be extendable, they will still be accessible, but no one will be able to add any values to them.

 

That is all you need to know about Business Central enums (in case you want to read more in-depth on enums, you can find more info here). From now on, forget about options and create enums instead. It will make your life easier since you can reuse Business Central enums and won’t have to write the same options many times.