Miklix

Creating a Lookup Field for a Financial Dimension in Dynamics 365

Published: June 30, 2022 at 5:34:01 PM UTC

This article explains how to create a lookup field for a financial dimension in Dynamics 365 for Operations, including an X++ code example.


The information in this post is based on Dynamics 365 for Operations, but most of it will also work for Dynamics AX 2012 (see below).

I was recently tasked with creating a new field in which it should be possible to specify a single financial dimension, in this case Product. Of course, the new field should also be able to lookup the the valid values of this dimension.

This is a bit more complicated than a regular lookup in a table, but if you know how, it's actually not too bad.

Fortunately, the standard application provides a convenient lookup form (DimensionLookup) that can be used for the purpose, if you just tell it which dimension attribute to lookup.

First, you need to create the form field itself. This can be based on a table field or an edit method, doesn't matter for the lookup itself, but in one way or another it must use the DimensionValue extended data type.

You then need to create an OnLookup event handler for the field. To create an event handler, right-click the OnLookup event for the field, then choose "Copy event handler method". You can then paste an empty event handler method into a class and edit it from there.

Notice: Most of this will work for Dynamics AX 2012 as well, but instead of creating an event handler, you can override the form field's lookup method.

The event handler must look something like this (replace form name and field name as necessary):

[
    FormControlEventHandler(formControlStr( MyForm,
                                            MyProductDimField),
                            FormControlEventType::Lookup)
]
public static void MyProductDimField_OnLookup(  FormControl _sender,
                                                FormControlEventArgs _e)
{
    FormStringControl   control;
    Args                args;
    FormRun             formRun;
    DimensionAttribute  dimAttribute;
    ;

    dimAttribute    =   DimensionAttribute::findByName('Product');
    args            =   new Args();
    args.record(dimAttribute);
    args.caller(_sender);
    args.name(formStr(DimensionLookup));
    formRun         =   classFactory.formRunClass(args);formRun.init();
    control         =   _sender as FormStringControl;
    control.performFormLookup(formRun);
}
Share on BlueskyShare on FacebookShare on LinkedInShare on TumblrShare on XShare on LinkedInPin on Pinterest

Mikkel Bang Christensen

About the Author

Mikkel Bang Christensen
Mikkel is the creator and owner of miklix.com. He has over 20 years experience as a professional computer programmer/software developer and is currently employed full-time for a large European IT corporation. When not blogging, he spends his spare time on a vast array of interests, hobbies, and activities, which may to some extent be reflected in the variety of topics covered on this website.