Search through blog..

Tuesday, October 30, 2012

Base Enums in Microsoft Dynamics Ax

Base Enums come in handy for categorizing data. One way of categorizing is to create a related table, such as item groups, which is used for grouping inventory items.
However, if you only need a fixed number of categories or the application users are not able to define the categories, you can use a base enum, such as Item type, used for defining the type for inventory items.


  • A base enum can have a maximum of 255 entries. A good example for multiple enteries in a base enum is LedgerTransTxt. But in general there will be very few entries.
  • The value of a base enum is stored in the database as an integer value. Entry values starts by default from zero and are consecutively numbered. The property EnumValue will show you the number stored in the database for an entry.
  • We can also manually enter a particular number for an entry in base enum, this can be done if the base enum property UseEnumValues is set to Yes.
  • System enums are located in the AOT under System Documentation/Enums, an example for system enum is ‘TableGroup’ represents the entries of the table property TableGroup.
  • To refer to a base enums entry in code, the name of the base enum is entered followed by a double colon. By pressing the second colon lookup will show the available entries.
Example(1): Below is a small example on how to use Base Enums in X++
static void DataDic_BaseEnum(Args _args)
{
InventTable inventTable;
;
while select inventTable
where inventTable.itemType == ItemType::BOM || inventTable.itemType == ItemType::Service
{
info(inventTable.itemId);
}
}
Here all items of Invent types BOM and Service are fetched.

Instead of checking whether the field itemType is equal to one of the enum entries specified, ‘greater than’ or ‘equal to’ the item type BOM can also be used. However, this is not recommended as it makes the code more difficult to maintain as the condition will not be defined and result will vary with every new data record entry.
while select inventTable where inventTable.itemType >= ItemType::BOM

We could also use the base enum entry numbers instead, but it would make your code more difficult to read and maintain.


Note: The first entry of a base enum normally has the value zero - which will return false, if the first entry is validated in an if-expression. This is also the reason why fields of the type enum should not be mandatory as the first entry of an enum would be considered as not valid.
Post a Comment