Extending the options – enums

A field of the option type is used in Dynamics 365 Business Central to define a field that provides a fixed and predefined list of values.

When you define an option field, you define the admitted values for that field in the following way:

field(5; LicenseType; Option)
{
OptionMembers = " ","Full","Limited";
OptionCaption = ' ,Full,Limited';
Caption = 'License Type';
DataClassification = CustomerContent;
}

In the preceding code, we can see that the OptionMembers property contains the predefined value for the field. Here, the License Type field contains three values (blank, Full, Limited), and blank (the first value) is the default one.

But what if you want to extend these options, for example, by adding a new license type called Teams? This isn't possible! Option fields cannot be extended.

To create an extendable option field, AL introduced the enum object. An enum is a type that consists of a set of named constants, and it can be extended from other extensions if you set the Extensible property to true, as shown here:

enum 50100 LicenseType
{
Extensible = true;
value(0; None) { }
value(1; Full) { }
value(2; Limited) { }
}

You can define a field so that it has the enum type in the following way:

field(50100; LicenseType; enum LicenseType)     
{
Caption = 'License Type';
DataClassification = CustomerContent;
}

This allows you to define a field that has the same behavior as an option: when a user clicks on that field, Dynamics 365 Business Central presents a list of possible values to choose from.

To extend the enum field from another extension and add a new possible value called Team, you need to create an enumextension object, as follows:

enumextension 50110 LicenseTypeEnumExt extends LicenseType
{
value(50110; Team)
{
Caption = 'Team License';
}
}

After that, your License Type field will have one more option value to choose from.

You can also use an enum object directly from AL code (as a variable):

var
LicenseType: enum LicenseType;
begin
case LicenseType of
LicenseType::Full:
//Write your code here…

You can also extend the TableRelation property of an enum value. For example, imagine you have the following table:

table 50120 LicenseDetail
{
fields
{
field(1; Id; Integer) { }
field(2; LicenseType; enum LicenseType) { }
field(3; LicenseDetail; Code[20])
{
TableRelation =
if (LicenseType = const (Full)) FullLicenseTable
else if (LicenseType = const (Limited)) LimitedLicenseTable;
}
}
}

In this table, we have a field called LicenseType (which is an enum) and a field called LicenseDetail, which has a tablerelation property (to the FullLicenseTable and LimitedLIcenseTable tables) based on the value of the enum field.

Another app could extend both the enum field and the table relation so that it can handle the new extended enum. Here's an example:

enumextension 50110 LicenseTypeEnumExt extends LicenseType
{
value(50110; Team)
{
Caption = 'Team License';
}
}

tableextension 50110 LicenseDetailExt extends LicenseDetail
{
fields
{
modify(LicenseDetail)
{
TableRelation = if (LicenseType = const (Team)) TeamLicenseTable;
}
}
}

Here, the new app creates the LicenseType enum extension (as we described previously) and creates a new tableextension object, where it modifies the TableRelation property of the LicenseDetail field by adding a new relationship to a TeamLicenseTable if the enum has the value of Team.

The combined TableRelation is always evaluated from the top down, so the first unconditional relationship will prevail. This means that you cannot change an existing TableRelation from table A to table B if the original field has a relationship with table A.

By using enums, you can extend all your option's values. We recommend using this new approach in your extensions if you want extensibility.

In this section, you've had a complete overview of the available objects in the AL Language extension. In the next section, we'll learn about some of the best practices when it comes to creating and handling an AL project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.178.133