Thursday, May 21, 2020
With the Microsoft Dynamics 365 Business Central 2019 wave 2 release, Microsoft made available a new data type – Enumeration (Enum).
Enum is a list that consists of a set of named constants. It can be used as table fields, local and global variables, and parameters.
Enum replaces the old Option data type and from now on, during development, instead of creating a new Option we have to create a new Enum. In addition, if you want to transition the solution from C/AL to AL, it is recommended to replace Option with Enum. Enums are more readable, reusable, and extendable compared to the Options data type.
How to define a new Enum
To define a new Enum, you have to create a new .al file in which you define the Enum as an object, and specify an ID and a name. In this object you list the values (each value is declared with a unique ID and a value, and you can add a caption for the value) of the Enum as follows:
Figure 1. Defining a new Enum
Enums are pure metadata and can't contain any kind of code.
After creating Enum, you might want to add additional values depending on changing business needs.
How to add additional values to the existing Enum
If you want to add one or more new values, you can just extend the existing Enum and you don’t need to find all the places where it is used that implement newly created values anymore. Of course, you can add more values to the original Enumeration list of values only if Enum is extensible (the extensible property is set to true). Extensible means that the Enum can be extended only in Extensions. Otherwise, you’ll get an error as shown in the screenshot below:
Figure 2. An example of how to extend Enum
How to replace Option with Enum
Microsoft intends to replace all Options with Enums in future releases. With the Microsoft Dynamics 365 Business Central 2020 wave 1 release, Microsoft has already added about 125 new Enums. That is why it is recommended to replace Options with Enums in your solutions as well. But before creating a new Enum, it is worth checking if it hasn’t already been created by Microsoft.
In the following screenshots, you can see how to change Option to Enum.
Figure 3. Change Option into an Enum as a field type
Figure 4. Change Option into an Enum as a variable
Figure 5. Change Option into an Enum as the former described variable used as Page field expression
How to implement warnings when creating a new Enum
Quite often you have to use Option as Integer, but conversion to and from Enum is stricter than for Options in C/SIDE. Therefore, not all places can be replaced exactly the same as with Option because warnings are received. Below are a few examples and tips on how to implement warnings:
- Record.TestField(Enum, Record.Enum::Value) Example - Employee.TestField(Team, Employee.Team::"A Team") ; -> Employee.TestField(Team, Employee.Team::"A Team".AsInteger()) ;
- Record.Option := Record.Enum Example - ItemChargeAssgntSales."Applies-to Doc. Type" := SalesLine."Document Type" ; -> ItemChargeAssgntSales."Applies-to Doc. Type" := SalesLine."Document Type".AsInteger() ;
- Record.Enum > Record.Enum::Value Example - Employee.Team > Employee.Team::"A Team" -> Employee.Team.AsInteger() > Employee.Team::"A Team".AsInteger()
- Record.Enum := Record2.Enum – 1 (1 – can be integer, decimal or even option) Example - Employee.Team := Employee2.Team - 1 -> Employee.Team := "Factory Team".FromInteger(Employee2.Team.AsInteger() - 1) ;
|- Not in use anymore|
|- Started to be used|
Frequently, it is enough to add .AsInteger() to the end of the Enum to deal with the warnings.
Apart from Enum, with the Business Central 2020 wave 1 release, Microsoft made Interfaces available. An Interface in AL is a syntactical contract that can be implemented by a non-abstract method.
What is Interface used for?
Interface is like a template that defines methods that have to be used by the functionality. It is used when you want to decide which capabilities need to be available for an object, while allowing actual implementations to differ. This allows for writing code that reduces the dependency on implementation details, makes it easier to reuse code, and supports a polymorphing way of calling object methods, which can be used for substituting business logic.
How to create and implement Interface
In the example below, you can see two new Interfaces created.
Figure 6. Creating new Interfaces
A new Interface object just declares the Interface name and methods (only name, parameters, return type, and visibility) without any code. All the necessary code must be described in one or more Codeunits that implement Interface. When you are implementing Interface in Codeunit, all methods must be defined, otherwise you’ll get an error.
Figure 7. Error of not all implemented methods of Interface
The Interface can be implemented by many different Codeunits with each having a different realization, but one Codeunit can also implement multiple Interfaces using comma separation.
Figure 8. Interface implementation.
As you can see, both Codeunits implement related methods differently.
How to use Interface implementation and how will it empower extendable Enums
In the screenshot below you can see how Interface enables a polymorphing way of calling object methods.
Figure 9. The use of Interface implementation
This example is not the best because you can figure out the extendable Enum issue which existed before Interface. Just imagine what happens if we extend Enum and add a value “C Team”. Method GetEmployeeProdDescription will return an empty value. But it can be fixed by defining which Interface the Enum values need to implement. To start with, we will create one more Codeunit to implement the new value of Enum.
Figure 10. Interface implementation
In the next step, we will extend Enum and for each value define which Interface to implement.
Figure 11. Linking Enum values with Interface implementation
As you can see, you don’t need to additionally implement Interface for Enum extension because it is already done from the original Enum. Finally, we will replace a Case statement.
Figure 12. Code improvements done using Enum with implemented Interface
The examples above illustrate that Interface implementation can be easily done.
Interface is a new Business Central 2020 wave 1 release feature with which you can create much more complex solutions with fewer code lines, requiring less effort to write them. C# and other programming languages have contained Enum and Interface for many years now. Many programmers who are familiar with those languages take advantage of its benefits. Introducing Enum and Interface to Business Central is great news for Business Central developers, who now can also reap the benefits of it.
Searching for more development tips and tricks? Visit our Technical NAV/BC blog category !