Business Central

AL Enums late discovering

Jalmaraz Profile Picture Jalmaraz 667  | 

Introduction

Praising for AL Enums could be the biggest obviousness we can write about Business Central in 2023. But when extensions arrived I had the feeling that Enum and options are roughly the same, especially if interfaces are not in a specific scenario.
And in addition, we have lots of existing C/SIDE code that still uses option type.
For reasons above, I underestimated Enums type, but I found two strong reasons to love it.

First: reusing, real use case

In our elaboration app somebody asked us to include in our elaboration document the feature of Default Posting Date of purchase documents. This feature set if you want to set by default posting date empty or work date in documents.
So, the old way with option type is create a new field in setting:
  • - Set the option members.
  • - Set the option captions.
  • - And later, set the local translations.
But only using existing Enum, work reduces to copy Enum field:
        field(15; "Default Posting Date"; enum "Default Posting Date")
        {
No need to re-define over and over members, captions or translations.

Second one: no destructive!!!

When you convert an option field to Enum field, this change is not considered destructive, even if the app is already in AppSource, and even if the converted field is part of the unique key of the table. The reason is that both are internally stored as integers in SQL databases.

Bonus: did you know?

With Enums you can set in pages, which members you want to allow. Is explained in post Dynamics 365 Business Central: Hiding enum values on the page (ValuesAllowed Property) | Dynamics 365 Lab (yzhums.com) from the notorious Yun Zhu blog.
Looks like this:

Next: Option to Enum utility

In the next post I will show the utility in my own VS Code extension JAL Variable Naming - Visual Studio Marketplace to convert Option to Enum.

Related stories