9 min read

Building a dynamic form

A standard approach for creating forms in Dynamics AX is to create and store form objects in the AOT. Using this approach, it is possible to achieve a high level of complexity. However, in a number of cases, it is required to have forms created dynamically. In the standard Dynamics AX application we can see that application objects, such as the Table browser form, various lookups, or dialogs, are built dynamically.

In this recipe, we will create a dynamic form. In order to show how flexible it can be, we will replicate the layout of the existing Customer groups form located in the Accounts receivable module. It can be opened from Accounts receivable | Setup | Customers.

How to do it…

Carry out the following steps in order to complete this recipe:

  1. In the AOT, create a new class called CustGroupDynamic with the following code:
  2. code 7

  3. In order to test the form, run the class. Notice that the form is similar to the one in Accounts receivable | Setup | Customers | Customer groups:

How it works…

We start the code by declaring some variables. Note that most of them begin with FormBuild, which is part of a set of the application classes used for building dynamic forms. Each of these types correspond to the control types manually used when building forms in the AOT.

Right after the variable declaration, we create a dictTable object based on the CustGroup table. We will use this object several times later in the code.

Then we create a new form object and set its AOT name by calling the following code:

code 8

The name is not important as this is a dynamic form, unless we are planning to save it in the AOT.

The form should have a data source, so we add one by calling the addDataSource() method on the form object and providing the previously created dictTable object.

code 9

Every form has a design, so we add a new design, define its style as a simple list, and set its title data source:

code

Once the design is ready, we can start adding controls from the code as if we were doing this from the AOT. The first thing to do is to add a strip action pane with its buttons:

code 10

Right after the action pane, we add an automatically expanding Grid control pointing to the previously mentioned data source. Just to follow best practice, we place the grid inside of a Group control:

code 11

Next, we add a number of grid controls pointing to the relevant data source fields by calling addDataField() on the grid object.

The last thing is to initialize and run the form. Here we use a recommended approach to create and run forms using the globally available classFactory object.

Adding a form splitter

In Dynamics AX, more complex forms consist of one or more sections. Each section may contain grids, groups or any other element. In order to maintain section sizes while resizing the form, the sections are normally separated by so-called splitters. Splitters are not special Dynamics AX controls; they are group controls with the properties modified so they look like splitters. Most of the multisection forms in Dynamics AX already contain splitters.

In this recipe, to demonstrate the usage of splitters, we will modify one of the existing forms that does not have a splitter. We will modify the Account reconciliation form in the Cash and bank management module, which can be opened from Cash and bank management | Bank accounts list page, by clicking on the Reconcile | Account reconciliation button in the action pane, and then selecting any of the existing records and hitting the Transactions button. In the following screenshot, you can see that it is not possible to control the sizes of each grid individually, and they are resized automatically using a fixed ratio when resizing the form:

In this recipe, we will demonstrate the usage of splitters by resolving this situation. We will add a form splitter in the middle of the two grids in the mentioned form. It will allow users to define the size of both grids to make sure the data is optimally displayed.

How to do it…

Carry out the following steps in order to complete this recipe:

  1. Open the BankReconciliation form in the AOT, and in the form’s design add a new Group control right after the ActionPane control with the following properties:
  2. table 1

  3. Move the AllReconciled, Balances, and Tab controls into the newly created group.
  4. Add a new Group control right below the Top group with the following properties:
  5. table 2

  6. Add the following line of code to the bottom of the form’s class declaration:
  7. code 12

  8. Add the following line of code to the bottom of the form’s init() method:
  9. code 13

  10. Override three methods in the Splitter group with the following code:
  11. code 14

  12. Change the following properties of the existing BankTransTypeGroup group:
  13. table 3

  14. Change the following property of the exiting TypeSums grid located inside the BankTransTypeGroup group:
  15. table 4

  16. In the AOT, the modified BankReconciliation form should look similar to the following screenshot:
  17. Now, to test the results, open Cash and bank management | Bank accounts, select any bank account, click Reconcile | Account reconciliation, choose an existing one or create a new bank statement, and click on the Transactions button. Notice that the form now has a nice splitter in the middle, which makes the form look better and allows resizing of both grids:
    1. Open the SalesCreateOrder form in the AOT, and set its Design property:
    2. table 5

    3. In order to test, open Sales and marketing | Sales orders | All sales orders, and start creating a new order. Notice that now the sales order creation form always stays on top:
  18. <

    How it works…

    Normally, a splitter has to be placed between two form groups. In this recipe, to follow that rule, we need to adjust the BankReconciliation form’s design. The AllReconciled, Balances and Tab controls are moved to a new group called Top. We do not want this new group to be visible to the user, so we FrameType to None. Setting AutoDeclaration to Yes allows us to access this object from the code. Finally, we make this group automatically expanding in the horizontal direction by setting its Width to Column width. At this stage, visual form layout does not change, but now we have the upper group ready.

    The BankTransTypeGroup group could be used as a bottom group with slight changes. We change its Top behavior to Auto and make it fully expandable in the horizontal and vertical directions. The Height of the grid inside this group also has to be changed to Column height in order to fill all the vertical space.

    In the middle of those two groups, we add a splitter. The splitter is nothing but another group, which looks like a splitter. In order to achieve that, we set the Height to 5, FrameType to Raised 3D, and BackgroundColor to Windows background. This group does not hold any other controls inside, therefore, in order to make it visible we have to set the HideIfEmpty property to No. The Column width value of the property Width forces the splitter to automatically fill the form’s width.

    Mouse events are handled by the SysFormSplitter_Y application class. After it has been declared in the form’s class declaration, we instantiate it in the object in the form’s init() method. We pass the name of the splitter control, the name of the top group, and the form itself as arguments when creating it.

    A fully working splitter requires three mouse event handlers. It is implemented by overriding the mouseMove(), mouseDown(), and mouseUp()event methods in the splitter group control. All arguments are passed to the respective member methods of the SysFormSplitter_Y class, which does all the work.

    In this way, horizontal splitters can easily be added to any form. The Dynamics AX application also contains nice examples about splitters, which can be found in the AOT in the Tutorial_ Form_Split form. Vertical splitters can also be added to forms using a very similar approach. For this, we need to use another application class called SysFormSplitter_X.

     

     

    Creating a modal form

    Quite often people who are not familiar with computers and software tend to get lost among open application windows. The same could be applied to Dynamics AX. Often a user opens one form, clicks a button to open another one, and then goes back to the first one without closing the second one. Sometimes this happens intentionally, sometimes not, but the result is that the second form is hidden behind the first one and the user starts wondering why it is not possible to close or edit the first form.

    Such issues can be easily solved by making the child form a modal window. In other words, the second form always stays on top of the first one until closed. In this recipe, we will do exactly that. As an example, we will make the Create sales order form a modal window.

    How to do it…

    Carry out the following steps in order to complete this recipe:

    How it works…

    The design of the form has a WindowType property, which is set Standard by default. In order to make a form behave as a modal window, we have to change it to Popup. Such forms will always stay on top of the parent form.

    There’s more

    We already know that some of the Dynamics AX forms are created dynamically using the Dialog class. If we look deeper into the code, we could find that the Dialog class actually creates a runtime form. This means that we can apply the same principle—change the relevant form’s design property. The following code could be added to the Dialog object and would do the job. The format is given as follows:

    code 15

    Here we get a reference to the form’s design, by first using the dialogForm() method of the dialog object to get a reference to the DialogForm object, and then we call buildDesign() on the latter object. Finally, we set the design property by calling its windowType() with an argument FormWindowType::Popup.

    Summary

    In this article, we looked at various aspects of building forms in Dynamics AX. We looked at dialogs and their events. We learned how to create a dialog and how to handle it. We also learned how to build a dyamic form, as well as learned how to create a modal form. We also looked at splitter, which is an important feature of Microsoft Dynamics AX 2012.

     


LEAVE A REPLY

Please enter your comment!
Please enter your name here