X++ tips, tricks and notes

List of table fields and types in CSV file

List of table fields and types in CSV file

I wrote a job today to create a list of fields in a table and each field’s data type. You specify a table name in the variables, and it he job exports a list of non-system fields in the format: “Displayed name” (label), “Technical Name” and “Data Type” to CSV file.

Notice (line 50 – 70) that it lists the possible enum values for enum fields. It also adds the string length in brackets for string fields.

//Tina van der Vyver
//makecreatereiterate.com
static void ListFields(Args _args)
{
    #File
    DictField           dictField;
    DictTable           dictTable;
    DictType            dictType;
    DictEnum            dictEnum;
    FieldId             fieldId;
    Types               type;
    TableName           tableName = tableStr(BatchJob);

    str                 enumValues, stringLength, stringType;
    int                 i;

    CommaTextIo         commaTextIo;
    FileIOPermission    permission;
    str                 fileName = strFmt(@"C:\%1.csv", tableName);
    ;
    permission = new FileIOPermission(fileName, #io_write);
    permission.assert();

    commaTextIo = new CommaTextIo(fileName ,#io_write);
    dictTable   = new dictTable(tableName2Id(tableName));
    fieldId     = dictTable.fieldNext(0);

    if (fieldId)
    {
        commaTextIo.write("Displayed name", "Technical Name", "Data Type");
    }

    while (fieldId)
    {
        dictField = new DictField(tableName2Id(tableName), fieldId);

        if (dictField && !dictField.isSystem())
        {
            type = dictField.baseType();

            switch (type)
            {
                case Types::String:
                    dictType        = new DictType(dictField.typeId());
                    stringLength    = (strFmt("%1[%2]",type, dictType.displayLength()));

                    commaTextIo.write(dictField.label(), dictField.name(), stringLength);
                    break;

                case Types::Enum:
                    dictEnum = new DictEnum(dictField.enumId());

                    for (i = 0; i < dictEnum.values(); i++)
                    {
                        if (i == 0)
                        {
                            enumValues = "Enumeration: ";
                        }
                        else
                        {
                            enumValues += "/";
                        }

                        if (dictEnum.value2Name(i))
                        {
                            enumValues += dictEnum.value2Name(i);
                        }
                        else
                        {
                            enumValues += dictEnum.value2Name(i);
                        }
                    }

                    commaTextIo.write(dictField.label(), dictField.name(), enumValues);
                    enumValues = '';
                    break;

                default:
                    stringType = strFmt("%1", type);
                    commaTextIo.write(dictField.label(), dictField.name(),  stringType);
                    break;
            }
        }
        fieldId = dictTable.fieldNext(fieldId);
    }
    CodeAccessPermission::revertAssert();
}

The example above exports the fields for the “BatchJob” table. In AX 2012 R3, it generates this CSV file:

List of fields



Leave a Reply

Your email address will not be published. Required fields are marked *