Friday 21 April 2023

Get the List of fields and methods available on table through x++

We might get some scenarios to get the list of objects or a list of elements available in a particular object in X++. Here is a sample piece of code to retrieve the details about the table object.

Whenever we try to get the metadata of an object (Tables, Table fields, table methods) we should keep in mind to use the below namespace in our code

using Microsoft.Dynamics.AX.Metadata.MetaModel;

To get list of tables

using Microsoft.Dynamics.AX.Metadata.MetaModel;
public class TableNamesList
{
    public static void getTableNames()
    {
       var tables = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetAllTables();
        var tablesEnumr = tables.GetEnumerator();
        while (tablesEnumr.MoveNext())
        {
            info(strfmt("%1", axTable.Name));
        }
    }
}


To get list of Fields from a table

public static void getFieldNames()
    {
        AxTable table =     Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetTable(tableId2Name(tablenum(SalesTable)));
       var fields = table.Fields;
        var fieldEnumerator = fields.GetEnumerator();
        while (fieldEnumerator.MoveNext())
        {
            info(strfmt("%1", field.Name));
        }
    }


To get list of methods available on a table

public static void getMethodNames()
    {
        AxTable table = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetTable(tableId2Name(tablenum(SalesTable)));
        var methods = table.Methods;
        var methodEnumerator = methods.GetEnumerator();
        while (methodEnumerator.MoveNext())
        {
            AxMethod method = methodEnumerator.Current;
            if(method.IsDisplay)
            {
                info(strfmt("Display method : %1", method.Name));
            }
            else
            {
                info(strfmt("Normal method : %1", method.Name));
            }
        }
    }

Similar to the above code, we can use the metadata .dll in X++ to get all the objects and its related details available in AOT 

No comments:

Post a Comment