Microsoft Dynamics AX FAQ

Microsoft Dynamics AX FAQ

How to override the event methods on dialog controls?

X++Posted by Studio Erudit team 2008-04-22 22:31

Overriding the event methods (e.g. modify, validate, selectionChange) on dialog controls is not as straight forward as it is on form controls, but the good news is that it is possible!

In order to override the event methods on dialog controls, the following needs to be done (for simplicity we assume that your class extends RunBase class) :

1) The method dialogPostRun() should be overridden and following two lines are added after the super() call:

_dialog.dialogForm().formRun().controlMethodOverload(true);

_dialog.dialogForm().formRun().controlMethodOverloadObject(this);


This will allow to call the event methods of your class.


2) The actual event methods should be added.

The format of the event method name is as follows: fld<ID>_1_<event name>


Please see the example below.

This method is called whenever a value of the dialog control with ID 900 is modified. In our case it is Employee ID field.


public boolean fld900_1_modified()

{
FormStringControl control = dialog.formRun().controlCallingMethod();

boolean isFieldModified;
;

isFieldModified = control.modified();

// every time the employee id is changed, update the employee name

if(isFieldModified)
{
dlgFldEmplName.value(EmplTable::find(control.text()).Name);
}

return isFieldModified;
}

You will also need to make sure that the control “Employee ID” gets the same control ID as used in the event method name (ID 900). This should be done at the time of adding the control to the dialog. Please see below:

protected Object dialog(DialogRunbase _dialog, boolean _forceOnClient)
{
;
dialog = super(_dialog, _forceOnClient);

// Add a new field by explicitly specifying the field id.
// This field id is used to create the field event methods (e.g. fld900_1_modified()).

dlgFldEmplId = new DialogField(dialog, typeid(EmplId), #dlgFlgEmplIdFieldNo);
dialog.addCtrlDialogField(dlgFldEmplId.name());

dlgFldEmplId.init(dialog);
dlgFldEmplId.label("@SYS81251");
dlgFldEmplId.helpText("@SYS81251");
dlgFldEmplId.value(emplId);


}

As you can see the macro #dlgFlgEmplIdFieldNo is used to assign the ID to the dialog control. The macro is defined in the classDeclaration and it equals 900.

That’s all what you need to do to be able to override the events on the dialog controls.

Here you can download a complete class which has the code used in the examples above.

  • Comments(4)

Fill in only if you are not real





The following XHTML tags are allowed: <b>, <br/>, <em>, <i>, <strong>, <u>. CSS styles and Javascript are not permitted.
Posted by Studio Erudit team 2008-04-25 10:26

The further investigation has shown that the previous statement below regarding the error "The value Fld900_1 is not found in the map." is only valid if the class is created on server AND it has RunOn property = Called From, otherwise the class will not be swapped to the client and the prompt will always be done on server and therefore this error will be occuring.

Posted by Studio Erudit team 2008-04-24 21:45

Thank you very much for your comments.

To gl00me: Adding a queryRun object does not break anything. The control still gets ID 900 and the respective event methods are being called.

To Vanya Kashperuk: You are right, an error occurs - "The value Fld900_1 is not found in the map.", BUT only if the prompt is performed on server. If the prompt is performed on client (by default it does, even if the class is created on server. The swapping from server to client is done in RunBase > promptPrim), then no error occurs. In fact, I can't think of any scenario where the prompt needs to be done on server, but if you know any, please us know.

Posted by Vanya Kashperuk 2008-04-23 09:37

if you class happens to run on Server, you will get a runtime error because of code you added to the dialogPostRun method.

Take a look at this project for solution + solution for prev. comment - adding fieldNames to dialogField objects of the dialog.

kashperuk.blogspot.com/2007/06/3-dialog-extensions.html

Posted by gl00mie 2008-04-23 07:21

Cool... Now take your RunBase descendant, override queryRun() and return something meaningful from it, override showQueryValues() and return true from it, then run your class again and see if your dialog control event handlers still work.
Hint: query ranges are created as dialog fields before you add your own DialogFields, and depending on the quality of ranges (that can berandom as user can add them) your DialogFields will get random internal Dialog numbers and eventually control names...