X++ code to get the Mandatory Fields in table in Ax 2012

Hi Friends, To day i would like to post a small job/Snippet which will be helpful in our day to day development to get the list of mandatory fields from a specified table.

Let me put it this way, some times we have to duplicate/Mimic a standard functionality without disturbing the standard flow/objects etc.etc..  in such cases this will be very helpful for us to get the list of mandatory fields from each table, instead of going to the each table and find out the properties.

Please find the below code snippet 

static void CheckMandatoryFieldsOnTable(Args _args)
{
        DictTable   dictTable;
        DictField    dictField;
        Counter      i;

        TableId       tableId = tablenum(SalesTable);//Table name
     
        dictTable = new DictTable(tableId);
 
        info(strFmt("Total No. of fields in: %1 are: %2\n\n\n",tableId2name(tableId),  dictTable.fieldCnt()));

        info('Mandatory Fields are following');

        for (i=1 ; i<=dictTable.fieldCnt() ; i++)
        {
            dictField = new DictField(tableId, dictTable.fieldCnt2Id(i));
            if (dictField.mandatory())
            {
                info(dictField.name());
            }
        }
}

Proud To Be a DAX Developer :-)