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.
Carry out the following steps in order to complete this recipe:
code 7
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.
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.
Carry out the following steps in order to complete this recipe:
table 1
table 2
code 12
code 13
code 14
table 3
table 4
table 5
<
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.
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.
Carry out the following steps in order to complete this recipe:
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.
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.
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.
I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…
Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…
Once we learn how to deploy an Ubuntu server, how to manage users, and how…
Key-takeaways: Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…
While developing a web application, or setting dynamic pages and meta tags we need to deal with…
Software architecture is one of the most discussed topics in the software industry today, and…