Axaptapedia is now maintained by AgileCadence For more information please click here.

Lookup Form

From Axaptapedia
Revision as of 04:35, 25 November 2013 by JanKjeldsen (Talk | contribs)

Jump to: navigation, search

Dynamics Ax usually creates required lookup forms on the fly, but it also allows the developer to create a custom lookup form and assign it as "FormHelp" to an extended datatype, so that it is automatically used. Why would you do that? There might be many reasons. Typically you need to display the data in a non standard order or you want to show only a subset of the data depending on some criteria. Here are the steps needed to create a proper lookup form.


In the following example I assume you create a lookup for table "xyz" and the ID field of that table is called "id".

Id Field must be string.

Create the form

First create a basic form. Usually this form contains not much more than a grid. But you might add additional controls. Then set the following properties on the datasource, so that the form can not be used for editing:

      AllowCheck:    No
      AllowCreate:   No
      AllowDelete:   No
      AllowEdit:     No
      AutoNotify:    No
      InsertAtEnd:   No
      InsertIfEmpty: No

Additionally set for the design the following properties to make it look like a proper lookup:

      AlwaysOnTop:   Yes
      Frame:         Border
      HideToolbar:   Yes
      WindowType:    Popup

Make it selectable

Now we need to tell the lookup form, which control will return the selected value. Lets assume you have a form with a grid and in that grid you have a control called "xyz_id". In this case you overwrite the init method of the form as shown below. Note that if you are using Axapta v3.0 or higher, then you can set the control's AutoDeclaration property to Yes and use the control name directly in the selectMode() call.

<xpp> public void init() {

   //FormControl xyz_id =;  // Set AutoDeclaration to Yes

} </xpp>

Make sure the form opens with the previously selected value

Strange enough the following is missing in most/all custom lookup forms, which you find in Axapta Standard. But it is required, if you want your lookup to mark the previously selected value as active record. Therefore overwrite the ExecuteQuery method of your datasource:

<xpp> public void executeQuery() {

   FormStringControl   callerControl   = SysTableLookup::getCallerStringControl(element.args());

} </xpp>

With a lot of pain I had top learn that the above will not work, if you use the standard query, which is automatically created by Axapta. This might be due to existing Dynalinks or other reasons, which I am not aware of. But it will work fine, if you create your own query for example in the Init method of the datasource. Here you can also define your custom sort or range criteria:

<xpp> public void init() {

   Query                q = new Query();
   QueryBuildDataSource qbds;
   qbds = q.addDataSource(tablenum(xyz));

} </xpp>

Make lookup react to search by wild card

The following you will find in the official documentation and also in a lot of Axapta examples. If you type in a standard Axapta StringEdit Control something like "abc*", then automatically the lookup will open and only the matching subset of records will be shown. You achieve this by overwriting the run method of the form:

<xpp> public void run() {

   FormStringControl   callerControl   = SysTableLookup::getCallerStringControl(element.args());
   Boolean             filterLookup    = false;
   // if lookup was called with filter, then supress autoSearch
   if (callerControl.text() && callerControl.hasChanged())
       filterLookup = true;
   // after call of super filter search manually by applying past filter
   if (filterLookup)

} </xpp>

Make Form Default lookup for an Extended Data Type

To have Axapta use your form by default every time a lookup is performed, you can set your form's name in the Extended Data Type's property called 'FormHelp'. You do not necessarily need a display menu item, the property is your actual form's name.

Use Form Default Lookup in Lookup Event

Use this code to add your custom lookup to the lookup event of the form. You may need to add the "caller" parameter as well depending on the lookup form.

<xpp> Args args = new Args(); FormRun itemLookUp;; args.parm(purchTable.OrderAccount); itemLookUp = new FormRun(args); itemLookUp.init(); this.performFormLookup(itemLookUp); </xpp>

So that's it. Now you have a wonderfully working custom lookup.

See also