If you need to force the user to select a value from a pre-defined list then an Option is the way to go. This recipe explains how to create an Option variable and access each of its values.
An Option is a field or variable that stores one value from a selectable list. In a form, this list will appear as a drop-down from which the user can select a value. The list of options is stored as a comma-separated string in the OptionString
property.
These values are accessed using the variable_name::option_name
syntax. The first line of the example assigns one of the possible values (Red) to the variable. Then we use a CASE
statement to determine which of the values was selected.
You can also access possible options in other ways. In a database, an Option is stored as an integer. Each Option corresponds to a specific number, starting with the number 1. In this case None=1, Red=2, Green=3, and Blue=4. You could write this code to perform the safe actions:
To reduce your development time, you can also use a shorthand notation to access the Option values. Again, the following code is exactly the same as that above:
When you close, save, and reopen the codeunit, the Option values will automatically be filled in for you. That is, both of these examples will look exactly like the first example once it has been saved and reopened. It is always best to write the code exactly as you want it to appear.
Using Options in documents
Option fields are prevalent throughout the NAV system, but most commonly on documents. In NAV, many documents share the same table. For example, sales quotes, orders, invoices, and return orders are all based on the Sales Header table. In order to distinguish between the types, there is an Option field called Document Type. Design table 36, Sales Header, to see the available options for this field.
Now, design codeunit 80, Sales-Post. Examine the OnRun
trigger. Early in the function, you will see the following code:
This is a common example of how Options are used in NAV. You can scroll through the codeunit to find more examples.