Enums are/will be the Option replacements for Business Central 365. Recently I had an occasion to use a few and get my feet wet, so to speak. As seems to be the case far too often, about 80% of the functionality you need is readily available, but the remaining 20% takes more work than it should.
In the case of Enums, you get a Text list of Names and an Integer list of associated Ordinal values, but you do NOT get a list of Captions. In the partial example Enum FourStates below, Default, OH and TX are the Names, 0, 1 and 2 are the Ordinals and space, Ohio and Texas are the captions. Note that Ordinals are the defined numeric values, NOT the indexes. Perfectly valid Ordinals in the example below could be 1, 5 and 7.
value(0; Default) { Caption = ' '; }
value(1; OH) { Caption = 'Ohio'; }
value(2; TX) { Caption = 'Texas'; }
If you define a Table or Page field as an Enum, then the captions are displayed in the dropdowns. To get the Caption you can use Format(EnumType::Name) but we needed to iterate all of the Captions for a given Enum.
After digging around in some blogs and the documentation, here is a summary of what I found.
First, there is a major limitation because Captions can ONLY be processed within the context of an Enum type and, at least as of now, the Business Central 365 SaaS solution has no support for generic Enums (e.g. EnumRef like RecordRef and FieldRef). This means a Text list of Captions must be created on an Enum by Enum basis.
However, once the Captions list has been created, then using the combination of Name, Ordinal and Caption Lists of [Text], you can then write general purpose code that will work for any Enum.
A Gotcha, when you use the TEnum code snippet in VS Code, it defaults the first element to 0. We have learned to either make that the Default as the partial code above does, or change it to 1. The reason for this is because if there is any chance that you will ever use an Enum with the StrMenu command, StrMenu defaults 0 as the Cancel return value. In that case, you could never select the 0 Ordinal Enum because ti would be treated as Canceled.
To create an instance of an Enum to use within your code, create a Var variable similar to MyChoices: Enum "Defined Choices";. If you also define a MyChoice: Enum: "Defined Choices", you can compare them with something like if MyChoice = MyChoices::FirstChoice then, etc.
Below is some sample code with a few different Enum styles along with a method that allows you to create a List of [Text] for the Captions. Again, that has to be coded for each specific Enum. a. Within VS Code, use AL Go to create a new HelloWorld app b. Change the app.json file Name and Publisher, I named mine EnumHelper NOTE: We always define a Publisher because you cannot filter for Default Publisher within SaaS c. Replace all of the code inside the HelloWorld.al file with the code below NOTE: To simplify things, everything below is all in the same file d. The code is a PageExtension for the Chart of Accounts Page and runs off the OnOpenPage trigger This allows the code to be easily called without requiring a bunch of setup code.
Here is the code that allows you to create a Captions list. The variable myOrdinal is an Integer and Flagged is a defined Enum. Note the Enum::EnumName, similar to Page::PageName or Database::TableName.
All of the code (sorry, it didn't format exactly correctly)
enum 50200 FourStates { Extensible = true;
}
enum 50201 Flagged { Extensible = true;
}
enum 50202 Randomized { Extensible = true;
}
enum 50203 ProcessFlowOptions { Extensible = true;
}
pageextension 50200 "Chart of Accounts EH" extends "Chart of Accounts" { var // Enum instance variables. myFlagged: Enum Flagged; myFourStates: Enum FourStates; myRandomized: Enum Randomized;
}
Enjoy