In Dynamics 365 for Finance and Operations, we can extend the functionality of public and protected methods without using event handlers. This can be achieved by applying Chain of Command (COC), which allows you to manipulate local and global variables in methods.
When implementing COC for form-level methods, Dynamics 365 checks for the existence of any extension methods. If the extension method exists, the COC method is executed first, and then the standard form methods are invoked using the next()
method.
Overriding a Method with COC
The following is an example of how to override form-level methods using COC in D365FO. We'll use the CustTable form as an example.
COC Code Example:
Explanation:
-
COC for Form Level Methods:
-
The
init()
method is overridden for the CustTable form using the COC technique. -
The
FormRun
object is used to get the form's runtime context, and the relevant data sources are fetched usingformHandler.datasource()
.
-
-
Accessing DataSource and Form Controls:
-
A
FormDataSource
object (custTableDataSource
) is created to represent the data source of the form. We use the cursor (custRecord
) to point to the currently selected record. -
We then retrieve the form control for the CustGroup field, using the form's design and control names.
-
-
Custom Business Logic:
-
Before calling the standard
next init()
method, we implement custom logic to set theCustGroup
to"40"
and manipulate the visibility of the form control accordingly.
-
-
Form Control Visibility:
-
The visibility of the control is dynamically changed based on the value of
CustGroup
. IfCustGroup == "40"
, the control is visible.
-
Best Practices with COC:
-
Maintain Readability: When working with COC, ensure that your custom logic is easy to read and doesn’t conflict with the base methods.
-
Use
next()
Wisely: Always call thenext()
method at the appropriate place in your overridden method to ensure the base functionality runs smoothly. -
Buffer Naming: Avoid using the same buffer names as in the standard methods. Create new buffer names to ensure uniqueness and prevent direct copying of the standard logic.
This method is highly useful in scenarios where you need to extend form functionality with custom business logic while ensuring that the base form behavior is preserved.
0 Comments