AX 7. Get information about table extension using Microsoft.Dynamics.Ax.Xpp.MetadataSupport

In AX 2012 we used DictTable class to get information about tables, table fields, indexes and field groups. However we cannot retrieve any information about table extensions using this class, so Microsoft.Dynamics.Ax.Xpp.MetadataSupport was introduced in AX 7.

Let’s look at simple job that will return all table extensions and all fields from table extensions for CompanyInfo table.

public static void main(Args _args)
{
    var axTableExtensions = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetTableExtensionsForTable(tableStr(CompanyInfo));
    System.Collections.IEnumerator axTableExtensionsEnumerator = axTableExtensions.GetEnumerator();

    while (axTableExtensionsEnumerator.moveNext())
    {
        Microsoft.Dynamics.AX.Metadata.MetaModel.AxTableExtension axTableExtension = axTableExtensionsEnumerator.get_Current();

        info(strFmt("Extension name %1", axTableExtension.Name));

        var axTableExtensionFields = axTableExtension.Fields;

        System.Collections.IEnumerator axTableExtensionFieldsEnumerator = axTableExtensionFields.GetEnumerator();

        while (axTableExtensionFieldsEnumerator.moveNext())
        {
            Microsoft.Dynamics.AX.Metadata.MetaModel.AxTableField axTableExtensionField = axTableExtensionFieldsEnumerator.get_Current();

            info(strFmt("Field name %1", axTableExtensionField.Name));
            info(strFmt("Mandatory %1", axTableExtensionField.Mandatory));
            info(strFmt("Allow edit %1", axTableExtensionField.AllowEdit));
        }
    }
}
Advertisements

AX 7. Accessing private\protected class methods and members from extension code.

All class member variables are protected by default in AX 7, so it is impossible to access them from extensions. It becomes a real problem when you try to extend classes like SalesLineType.

For example, we want to add custom logic on sales line insert event. Call to super() is commented out so we cannot create pre or post event handlers. We may try to create event handlers for SalesLineType.insert() mehtod, but we won’t get access to salesLine buffer because this variable is protected.

There are two options: use overlaying or use reflection.

Overlaying is preferable approach, but today we will talk about reflection to explain this option.

Reflection is usually used for unit testing in case you need to cover protected or private code and it is hard to call this code using public API.

It has bunch of disadvantages:

  • It breaches entire basis of OO programming.
  • Slow performance.
  • Possible issues with future updates. Private methods could be changed at any time.

However, once you may get into situation where it could be the only option so it’s better to know about this possibility.

Let’s try to change some fields on sales line insert using reflection.

Create new event handler class for SalesLineType and subscribe to post insert:

using System.Reflection;

///
<summary>
/// Handles events raised by <c>SalesLineTypeEventHandler</c> class.
/// </summary>

public class SalesLineTypeEventHandler
{
    [PostHandlerFor(classStr(SalesLineType), methodStr(SalesLineType, insert))]
    public static void SalesLineType_Post_insert(XppPrePostArgs _args)
    {
        SalesLineType salesLineType = _args.getThis();

        var bindFlags = BindingFlags::Instance | BindingFlags::NonPublic;

        var field = salesLineType.GetType().GetField("salesLine", bindFlags);

        SalesLine salesLine = field.GetValue(salesLineType);

        if (salesLine)
        {
            salesLine.MyNewField = 42;
            salesLine.doUpdate();
        }
    }
}

Also we can call private or protected method:

var bindFlags = BindingFlags::Instance | BindingFlags::NonPublic;

var methodInfo = salesLineType.GetType().GetMethod(methodStr(SalesLineType, checkQuantityUpdate), bindFlags);

if (methodInfo)
{
    methodInfo.Invoke(salesLineType,  new System.Object[0]());
}

You can read more about reflection on msdn

Thanks to Simon Buxton for rising this on yammer.