Dynamics Ax 2009 – Dynamically adding a button to a form
I was recently working on an Ax2009 project where users wanted to be able to dynamically add buttons to their forms based on data they set up. The idea was that they would not need to go to the AOT to add new buttons.
I’m sharing my findings in this article. I understand that none of this works in Dynamics 365 for Operations and Finance – in fact I found that none of the mentioned classes exist anymore. It would be interesting, in the comments section to hear of anyone who has had similar experiences in either version of Ax.
The Challenge
Users wanted a way that they can type a macro – say Dos commands. These macros should be assigned to a form in Ax. When the form is open, a button should show up which would execute the macro. The function should be capable of using data from the record currently highlighted on the form in order to use it in the macro.
The Solution
The key to the solution is in analyse parts of the knowledge management module which has a similar functionality.
- Within Ax 2009 there is a class called SysSetupFormRun that is executed whenever a form if launched.
- In the new method the class calls this.updateKMButtonControls() which calls KMActionMenuButtonAuto.Create().
- This cycles through table kmActionAuto and calls this.createbutton() for every row.
- As KMActionMenuButtonAuto extends from CCMenuButtonAuto, the createbutton() method from CCMenuButtonAuto is instead called and this creates a button on the form
To show this diagrammatically:
SysSetupFormRun.new()
SysSetupFormRun.updateKMButtonControls()
KMActionMenuButtonAuto.Create()
CCMenuButtonAuto.CreateButton()
Once you get that logic the rest is simple:
- Create a class extending CCMenuButtonAuto
- Within the class have a create() method that calls the createbutton() when you want to create a button
- Back in the new() method of SysSetupFormRun call your class
Passing data
The standard createbutton method only takes a button and text. It is therefore not possible for it to interact with the data on the source form. This can be fixed as follows:
- Change the definition of the button to include the datasource:
void createButton(str buttonId, str buttonText, TableName _tableName = formBuildDataSource.name())
- Set the button datasource
formBuildFunctionButtonControl.dataSource(_tableName);
When calling the CreateButton method you can now pass it the datasource you want to link. This logic can also be used if you want to assign other attributes as well instead of just name and id.
*This post is locked for comments