Understanding Base Enums in D365 F&O - Key Concepts and Usage
Table of Content:
Base enums in D365 F&O
A base enum is a list of literals with an assigned value. You will need to create base enumerations in Visual Studio before you can use them in X++ code. Enum values are represented internally as integers, and they are stored in the database as a number instead of the value, which is useful because it saves space. The first literal has the number 0, the next has the number 1, and so on.
Enums can be used as integers in expressions when you are writing code. Each literal or enumeration is assigned a text value through a label and name property. That property is displayed to users in the finance and operations user interface. The system automatically displays this conversion in the user interface, so no more programming is required on the forms that display to the user.
How to create Base enums in D365 F&O -Step by Step
Creating an Enum
To create an enum
- Expand the Data Dictionary node in the AOT. / or Add new Item in Visual Studio by right clicking on the project, the select Data types then Select Base enum.
- Right click the Base Enums node and select New Base Enum.
- Rename the enum.
- The literals in the enum are called elements. Right-click the enum and select New Element.
- Rename the element.
- Add as many additional elements as you need.
By default, elements take values in the order in which they are created. The first element has the value 0, the second element has the value 1, and so on. However, you can explicitly assign a particular integer value to an element by using the EnumValue property.
The following screenshot shows how to add a base enum to your project in the Solution Explorer window.
Right click on your project And click on add a New Item. | |
Select Data Types then select Base Enum. Give appropriate name after that click on Add. | |
PresenceStatus Enum |
Examples
Many enumerable types are built into the standard finance and operations apps. Each enum has various properties that control the appearance and function of the base enum.
For example, the enum NoYes has two associated literals, where No has the value 0, and Yes has the value 1. Typically, this type of enumeration appears as a selectable option in the finance and operations user interface. This is also referred to as the Boolean primitive data type.
A base enum can also appear as a drop-down menu in the user interface, where a user is presented a list of options and one can be selected from the list.
A base enum can be displayed as various buttons. When each button is selected, the user interface updates with new values or information that is related to the selected button.
Properties and elements
Defining properties for a base enum allows you to modify the look of the base enum in the finance and operations apps user interface. While the process of adding a base enum to a project is constant, you can adjust the display length, style, label, and behaviors of the enum by using the Properties window.
Elements that are added to a base enum are stored in the database as a numeric value. The first element that you add is stored as the value 0, the next element is stored as the value 1, and so on. You can use these numeric values as integers when writing X++ code. To give the numeric value a meaningful display name in the user interface, you will edit the Label property in the Properties window.
Declaration of Enums
You must create an enum type in the AOT before you can declare it.
Enum declaration |
= |
enumname Variable { , Variable } ; |
Variable |
= |
identifier [ option ] |
Option |
= |
[ [ Length ] [, Memory ] ] | initialization |
//A NoYes enum NoYes done; //An array of Criteria enums Criteria crit[100]; // User-defined enum PresenceStatus status; status = PresenceStatus::DoNotDisturb;
Example 1
Access PresenceStatus
Enum using code.
internal final class RunnableClassCodeAccessEnum { /// /// Class entry point. The system will call this method when a designated menu /// is selected or when execution starts and this class is set as the startup class. /// /// The specified arguments. public static void main(Args _args) { PresenceStatus status; status = PresenceStatus::DoNotDisturb; info("strFmt("%1", status ")); } } }
Output:
Do Not Disturb
Example 2
internal final class RunnableClassCodeAccessEnum { /// /// Class entry point. The system will call this method when a designated menu /// is selected or when execution starts and this class is set as the startup class. /// /// The specified arguments. public static void main(Args _args) { PresenceStatus status; status = PresenceStatus::DoNotDisturb; switch (status) { case PresenceStatus::AppearAway: info("Hi All, I am appearing away"); break; case PresenceStatus::AppearOffline: info("Hi All, I am appearing offline."); break; case PresenceStatus::Available: info("Hi All, I am available."); break; case PresenceStatus::BeRightBack: info("Hi All, Be right back."); break; case PresenceStatus::Busy: info("Hi All, I am busy"); break; case PresenceStatus::DoNotDisturb: info("Hi All, Please don't disturb me."); break; } } }
Output:
Hi All, Please don't disturb me.