Base Enums in AX 2012

Description:-

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.
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
static void RetreiveEnumValue(Args _args)
{
    DictEnum denum;
    A_StudentDemoTable  A_StudentDemoTable;
    int i;
    //How to Check Values in Enums
    denum=new DictEnum(enumName2Id("StudentResult"));
    info("-----------Enum Values-------------");
    for(i=0;i<denum.values();i++)
    {
        info(strFmt("%1 - %2",denum.index2Label(i),denum.index2Value(i)));
    }
    //How to Use Enum
    info("--------------------------------------");
    info("-----------Student Results-----------");
    while select * from A_StudentDemoTable where A_StudentDemoTable.Result == StudentResult::FAIL
    {
        if(A_StudentDemoTable.Result)
        {
            info(strFmt("StudentID - %1, Result - %2",A_StudentDemoTable.Student_ID,A_StudentDemoTable.Result));
        }
    }
    info("--------------------------------------");
}

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.

Related Posts

Previous
Next Post »