
When developing forms in Dynamics 365 Finance and Operations (D365FO), you may find yourself needing to programmatically access specific form controls based on their names. This can be useful for dynamically manipulating control properties, applying custom logic, or performing validations.
In this article, we’ll demonstrate how you can retrieve form control objects directly by their control names using controlName().
The Example Code
The following simple example demonstrates how to achieve this:
Example Code Snippet
element.design().controlName('MyFormControlName');
Explanation
- element.design()
This method retrieves the design of the form. Theelementkeyword refers to the form itself when used in form methods, providing access to its elements and properties. - controlName()
ThecontrolName()method allows you to specify the control you wish to retrieve by its name. In the example,'MyFormControlName'is the name of the control you want to access. By passing this string tocontrolName(), you can directly get a reference to the control. - Accessing the Control
Once the control object is retrieved, you can access and modify its properties, such as visibility, text, and other attributes. Here’s an extended example showing how to change a property of the retrieved control:
FormControl myControl = element.design().controlName('MyFormControlName');
if (myControl)
{
myControl.enabled(false); // Disabling the control as an example
}
Use Cases and Practical Applications
1. Dynamically Modifying Control Properties
You can enable, disable, hide, or modify other properties of controls dynamically at runtime based on user input, data values, or other conditions. For example, disabling a button when certain criteria are not met.
2. Custom Form Logic
Accessing controls by name is beneficial when building custom logic around user interactions. You can add conditional behavior to controls, perform calculations, or trigger events based on form state.
3. Improved Readability and Maintainability
By referencing controls by their names, you can improve the readability and maintainability of your form logic, especially if you have multiple controls to manage.
4. Validation
Accessing controls by their names allows for form validation and ensuring data integrity based on control values before submitting or processing data.
Example Scenario
Let’s say you have a form with a button control named 'SubmitButton' and you want to disable it based on a certain condition. Here’s how you might do it:
FormControl submitButton = element.design().controlName('SubmitButton');
if (submitButton)
{
if (someCondition)
{
submitButton.enabled(false); // Disable the button if the condition is met
}
else
{
submitButton.enabled(true); // Enable the button otherwise
}
}
Key Considerations
- Ensure that the control name you are referencing exists on the form; otherwise, you may get a
nullvalue. - The
controlName()method works best when you know the exact name of the control. If there is any potential variability, you might need additional logic to handle those cases.
Conclusion
Accessing form controls by their names in D365FO using element.design().controlName() is a straightforward and powerful way to manipulate form elements programmatically. This approach allows for dynamic customization and control of form behavior, enhancing user experience and meeting complex business logic requirements.
Leave a comment