Lookup Form

From Axaptapedia
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.

Assumptions[edit]

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[edit]

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[edit]

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 = element.design().control(control::xyz_id);  // Set AutoDeclaration to Yes
   super();
   element.selectMode(xyz_id);

} </xpp>

Make sure the form opens with the previously selected value[edit]

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());
   super();
   xyz_ds.findValue(fieldnum(xyz,id),callerControl.text());

} </xpp>

Disable Dynalink in the Init method of the datasource. Here you can also define your custom sort or range criteria:

<xpp> public void init() {

   QueryBuildDataSource qbds;
   super();
   qbds = this.query().findDataSource(tablenum(xyz));
   qbds.clearDynalinks();
   qbds.addSortField(fieldNum(xyz,some_other_field));

} </xpp>

Make lookup react to search by wild card[edit]

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;
       xyz_ds.autoSearch(false);
   }
   super();
   // after call of super filter search manually by applying past filter
   if (filterLookup)
   {
       xyz_ds.research();
       xyz_ds.filter(fieldnum(xyz,id),callerControl.text());
   }

} </xpp>

Make Form Default lookup for an Extended Data Type[edit]

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[edit]

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.name(formstr(POVendItemLookup)); 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[edit]

Lookups