How to apply color formatting to a specific form control in a grid based on a business condition in X++

 

Purpose

The purpose of this document is to demonstrate how to dynamically apply color formatting to a specific form control in a grid based on a business condition in X++.

In this scenario, the requirement is to highlight the AvailOrdered field (display method) in the InventOnhandItem form:

  • Show Red when the value is negative
  • Show Green when the value is positive

Product

Microsoft Dynamics 365 for Finance and Operations (D365 FO)


Development Approach

Customization through X++ code by extending the form control.
The logic will be implemented at the control level to dynamically evaluate values and apply colors during runtime.

Since the field is a display method, the color formatting must be applied using control methods like:

  • displayOption()
  • or control override methods

Development

Below is the X++ implementation to apply color dynamically to the specific control:

Step 1: Override displayOption method on Form Data Source

[ExtensionOf(formDataSourceStr(InventOnhandItem,InventSum))]

final class InventOnhandItem_Inventsum_Extension

{

 

    public void displayOption(Common _record, FormRowDisplayOption _options)

    {

        next displayOption(_record, _options);

        real availOrderedValue;

        FormControl ctrl;

 

        // Call your display method from DS to get real value      

        availOrderedValue = this.availOrderedCalculated(_record);

 

        // Get the specific form control by name (exact control name!)

        ctrl = this.formRun().design().controlName(identifierStr(AvailOrdered));

 

        if (ctrl)

        {

            if (availOrderedValue < 0)

            { 

                _options.affectedElementsByControl(ctrl.id());

                _options.backColor(WinAPI::RGB2int(255,0,0)); // red

            }

            if (availOrderedValue > 0)

            { 

                _options.affectedElementsByControl(ctrl.id());

                _options.backColor(WinAPI::RGB2int(0,204,0)); // green

            }

        } 

    } 

}

Result:

 

Comments