Change Editability of a Form Control

Posted by Sam on Tuesday, May 31, 2022

Sharing 2 different approaches of customizing the editability of a form control in D365FO

Request

I need to extend ProjInvoiceProjIdTab form control to be non-editable in ProjTable form.

Investigation of the standard design

I have looked into the ProjTable form and found that by default, the field ProjInvoiceProjIdTab is enabled and editable as below.

ProjInvoiceProjIdTab control in ProjTable form design

For most of the elements, you can extend the form and change the elements’ properties to modify their behaviour, for example, you want it to be non-editable, you can just set the property Allow Edit=No.

However, for ProjInvoiceProjIdTab, this is not enough. Looking more into the code, we have found that the field ProjInvoiceProjIdTab has property Auto Declaration = Yes. This means that this standard field is also controlled by the code (reference: Form Control Properties |D365FO|AX). So even if we set the property Allow Edit = No, the code still sets it to be editable.

If you go deeper into the form’s code, you’ll see that its value is controlled by code in setFieldAccess().

setFieldAccess() in ProjTable form code has higher control over the editablity than Allow Edit property’s value in the form design.

setFieldAccess() in ProjTable form code has higher control over the editablity than Allow Edit property’s value in the form design.

Solutions

  1. Form Design approach: create extension of the form and change the form design with a new field.

    The 1st approach is to add a new control copy of ProjInvoiceProjIdTab so that the copy still has the same value. However, this new copy will have Allow Edit = No and Auto Declaration = No.

    The 1st approach is to create a new control that has Allow Edit = No and Auto Declaration = No

    If you don’t want to see standard field in UI anymore, you should hide it by setting Visible = No. Then your end users will only see your new field and navigate to the object without being able to edit.

    Setting Visible = No for the standard control to hide it away

    The result should be like this. It’s readable but greyed out so not editable.

    Example screenshot of the result

  2. Chain of Command (CoC) approach:

    We will need to create a CoC class to extend the ProjTable form. In the extension class, we can customize the logic for the SetFieldAccess() method and set ProjinvoiceProjIdTab.allowEdit(false). Now the standard form control becomes non-editable but still navigable. ****

    Generally, this approach is more common.

    [ExtensionOf(formStr(ProjTable))]
    final class SamTestProjTable_Extension
    {
        public void setFieldAccess()
        {
            *// must always call next() for CoC*
            next setFieldAccess();
    
            if (!this.isInCreateMode())
            {
                // this applies directly to the standard form field
                projInvoiceProjIdTab.allowEdit(false); 		
            }
        }
    }