Saturday 6 September 2014

Read properties of table fields using X++ code

Hi Friends,
The below piece of X++ code can be used to read properties of table fields.
To demonstrate, the below job will check the mandatory property of the fields of a table. This comes handy during technical analysis. It can be time consuming to check the property of each field manually.

The code uses DictTable and DictField classes to iterate through the table fields and it's properties. This can also be used to check other properties of the fields of a table.


static void checkProperties(Args _args)
{
    DictTable       dictTable;
    DictField       dictField;
    int             i, cnt;
   
    dictTable = new DictTable(tableNum(CustTable));
    cnt = dictTable.fieldCnt();
    for (i= 1; i<=cnt;i++)
    {
        dictField = new DictField(tableNum(CustTable),dictTable.fieldCnt2Id(i));
        if (dictField.mandatory())
        {
            info (strFmt("Field %1 is mandatory.",dictField.label()));
        }
    }
     
}
Similar to the way we have checked the property of field using dictField object in the above job, we can also read properties of any table using the dictTable object.

These classes are used at lot of places in standard AX code to iterate through table structures. Some common scenario are when we need to show list of table/fields in lookups.