AX 2012: Loop through all the fields of a table in X++

This is how we can loop through all the fields of a table in X++:

static void Job1(Args _args)
{
    SysDictTable    dictTable = new SysDictTable(tableNum(PurchLine));
    SysDictField    dictField;
    TreeNode        treeNode;
    FieldId         fieldId = dictTable.fieldNext(0);

    while (fieldId)
    {
        dictField = dictTable.fieldObject(fieldId);

        if (dictField.isSql() && !dictField.isSystem())
        {
            treeNode = dictField.treeNode();
            info(strFmt("%1 | %2 | %3",
                dictField.name(),                                               // Field name
                treeNode.AOTgetProperty("Label"),                               // Label id
                SysLabel::labelId2String(treeNode.AOTgetProperty("Label"))));   // Label string
        }

        fieldId = dictTable.fieldNext(fieldId);
    }
}
Advertisements

2 thoughts on “AX 2012: Loop through all the fields of a table in X++

  1. It can be useful to get a label used in UI, but not a label defined on table field. To achieve this – dictField.labelLabel() should be used instead of treeNode.AOTgetProperty(“Label”).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s