How to refer to form controls in code without setting the AutoDeclaration property in Dynamics AX?
Hello readers,
On a Dynamics AX form, if you want to refer to a particular control in code, the surest way you would take is to set the AutoDeclaration property to Yes. This will let you refer the control in code. There is nothing wrong with this approach and it is quite common in standard Dynamics AX codebase.
But in some cases, it is preferred not to use this approach. So what is this case?
Consider a scenario where you are customizing a standard form. You want to disable a particular field based on a field value.
To achieve this,
- You will set the AutoDeclaration property of the control to Yes
- In the active method of the datasource, you will disable the control based on a condition.
Now, the active() method's modification can be easily seen in the AOT but the change in property isn't readily noticed unless you perform a layer compare.
Normally, in cuztomization it is a best practise to add customization tags to show what part is customized code and what is standard. But there is no way we can add a customization tag to properties that have changed.
Coming back to the main topic, so how do we refer to a control in code without setting AutoDeclaration to Yes.
The answer lies in the control() method of formRun class.
So let us say my control name is AccountNum and form name is Address. So my code will look something like this,
In Dynamics AX 2012, we have a new method controlId() on the formRun class. This in conjunction with the new intrinsic method formControlStr() can be used to get the control id.
So the above line of code will be written as following in Dynamics AX 2012
I hope this post was helpful. Please leave your comments here.
Thats all for today. Do check back soon as we have lots of new things coming up.
On a Dynamics AX form, if you want to refer to a particular control in code, the surest way you would take is to set the AutoDeclaration property to Yes. This will let you refer the control in code. There is nothing wrong with this approach and it is quite common in standard Dynamics AX codebase.
But in some cases, it is preferred not to use this approach. So what is this case?
Consider a scenario where you are customizing a standard form. You want to disable a particular field based on a field value.
To achieve this,
- You will set the AutoDeclaration property of the control to Yes
- In the active method of the datasource, you will disable the control based on a condition.
Now, the active() method's modification can be easily seen in the AOT but the change in property isn't readily noticed unless you perform a layer compare.
Normally, in cuztomization it is a best practise to add customization tags to show what part is customized code and what is standard. But there is no way we can add a customization tag to properties that have changed.
Coming back to the main topic, so how do we refer to a control in code without setting AutoDeclaration to Yes.
The answer lies in the control() method of formRun class.
So let us say my control name is AccountNum and form name is Address. So my code will look something like this,
element.control(control::AccountNum).enabled(false);The control:: enum enumerates all controls on the given form. The only problem is that you wont get an intellisense on the control names. Just type it and then compile.
In Dynamics AX 2012, we have a new method controlId() on the formRun class. This in conjunction with the new intrinsic method formControlStr() can be used to get the control id.
So the above line of code will be written as following in Dynamics AX 2012
// This is the line in Dynamics AX 2009 element.control(control::AccountNum).enabled(false); // And this is how it should be written in Dynamics AX 2012 element.control(element.controlId(formControlStr(Address, AccountNum))).enabled(false);Although, there is nothing wrong in setting AutoDeclaration to Yes for customization, it is a cleaner approach to just reference the control in code using the above method.
I hope this post was helpful. Please leave your comments here.
Thats all for today. Do check back soon as we have lots of new things coming up.
Comments
This has the added advantage that it will work for controls added by the user, and when the field is added twice, say in the grid and in a tab.
The syntax:
address_ds.object(fieldNum(Address,AccountNum)).enabled(false);
Thanks for pointing this out. It is preferrable to directly act on the datasource field if it is available.
For other controls which are not bound to a datasource, we can use the element.control approach.