Options and enums

Completed

The Option data type is a zero-based enumerator type; therefore, you can convert option values to integers. For that reason, an Option data type is a numeric data type because the option values are stored as integers.

To define a variable of type Option, you can't use the OptionMembers property that's used on a field of data type Option. You need to list the available options as a comma-separated list after your variable definition.

Example of using the option data type in AL.

If you want to reuse the same Option type in other objects (like other codeunits, pages, or tables), you have to redefine all available values. Later, if you decide to add an extra value, you need to modify all objects with this extra value. Options in a table aren't extendable with a table extension.

A better approach would be to use the Enum object. An enum is a separate object with its own number and name. You can use an Enum object in every other object without the need to redefine it. The Enum object can also be extended with enum extensions.

An enumeration type, also known as an enum in programming, is a keyword used to declare a type that consists of a set of named constants. The list of named constants is called the enumeration list. Enums can be used as table fields, local and global variables, and parameters.

To declare an enum in AL, you must specify an ID and a name. The enumeration list consists of values and each of the values are declared with an ID and a value. The value ID is the ordinal value on the enumeration list and must be unique. When the enum values are displayed in the UI, they're sorted by the order of declaration. In addition, if extension B extends extension A, the enum values declared in extension A are displayed before the enum values declared in extension B.

Example of using the Enum object type in AL.

While enums and enumextension objects have object IDs, these aren't enforced by the license. In previous versions they reused the range for tables, and were checked against the license at deployment time, but this is no longer the case. Uniqueness validation is now enforced during installation, which will fail if an enum object ID clashes with an already installed enum. Thus, as always, it's important that you use object IDs in your assigned range. This is enforced for AppSource apps, but not for per-tenant extensions, or on-premises. The enum doesn't have to use the same ID as the table it's put on.

Only enums with the Extensible property set to true can be extended.

When creating captions for enums, it's important that the caption doesn't contain a comma. Having a comma in the caption, such as Caption = 'Diamond Level, with bonus', can display over multiple lines in the UI. This behavior also causes that the actual value selected by the user in the UI, doesn't correspond to the value, which is saved in the database.

An AppSourceCop warning is triggered if .xlf files contain commas in enum captions.

Enums can be extended in order to add more values to the enumeration list in which case the Extensible property must be set to true. The syntax for an enum extension, which extends the Loyalty enum with the value Diamond, is shown below.

	enumextension 50130 LoyaltyWithDiamonds extends Loyalty
	{
	    value(50130; Diamond)
	    {
	        Caption = 'Diamond Level';
	    }
	}