Header Ads Widget

COC for Form Level Method in D365FO

 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:

x++
[ExtensionOf(formstr(CustTable))] final class CustTableForm_Extension { public void init() { // COC for CustTable form - start FormRun formHandler = this as FormRun; // Getting the data source and cursor (table buffer) FormDataSource custTableDataSource = formHandler.datasource(FormDatasourceStr(CustTable, CustTable)); CustTable custRecord = custTableDataSource.cursor(); // Accessing form controls (buffer controls) FormControl custGroupControl = formHandler.design().controlName(FormControlStr(CustTable, CustGroup)); // Custom logic before calling next() next init(); // Call the standard init method // Custom business logic: Set default customer group and modify control visibility custRecord.CustGroup = "40"; custGroupControl.visible(custRecord.CustGroup == "40"); // Show the control based on the CustGroup } }

Explanation:

  1. 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 using formHandler.datasource().

  2. 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.

  3. Custom Business Logic:

    • Before calling the standard next init() method, we implement custom logic to set the CustGroup to "40" and manipulate the visibility of the form control accordingly.

  4. Form Control Visibility:

    • The visibility of the control is dynamically changed based on the value of CustGroup. If CustGroup == "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 the next() 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.

Post a Comment

0 Comments