How to iterate over the elements of an enum from X++ code in Dynamics AX 2012

The information in this post is based on Dynamics AX 2012 R3. It may or may not be valid for other versions.

I was recently creating a form that needed to display a value for each element in an enum. Rather than manually creating the fields (and then needing to maintain the form if the enum is ever modified), I decided to implement it dynamically so that it would automatically add the fields to the design at run time.

However, I soon discovered that actually iterating over the values in an enum, while easy enough once you know how, is a bit confusing.

You obviously need to start with the DictEnum class. As you will see, this class has several methods for obtaining information such as name and label from both index and value.

The difference between index and value is that index is an element’s number in the enum, if the enum’s elements were numbered sequentially starting from zero, while value is the element’s actual “value” property. As most enums have values numbered sequentially from 0, the index and value of an element will often be the same, but certainly not always.

But how do you know which values an enum has? This is where it gets confusing. The DictEnum class has a method called values(). You might expect this method to return a list of the enum’s values, but that would obviously be too easy, so instead it returns the number of values the enum contains. However, the number of values has nothing do with the actual values, so you need to use this number as a basis for calling the index-based methods, not the value-based once. If only they had named this method indexes() instead, it would have been less confusing 😉

Also bear in mind that enum values (and apparently these “indexes”) start with 0, unlike array and container indexes in X++, which start with 1, so to loop over the elements in an enum you could do something like this:

DictEnum dictEnum = new DictEnum(enumNum(SalesStatus));
Counter  c;

for (c = 0; c < dictEnum.values(); c++)
    info(strFmt('%1: %2', dictEnum.index2Symbol(c),

This will output the symbol and label of each element in the enum to the infolog.