Get mandatory fields

Hi Folks,

I would like to get mandatory fields which are set to be required by the user (right click → personalize), is there any method on the form level to get these required fields?

Yes, you can get the list of mandatory fields which are set to be required by the user on the form level by using the following code in the form’s codeunit:

fieldList.SETRANGE(“System”,FALSE);
fieldList.SETRANGE(“Editable”,TRUE);
fieldList.SETRANGE(“Enabled”,TRUE);
fieldList.SETRANGE(“Relevant”,TRUE);
fieldList.SETRANGE(“Mandatory”,TRUE);
fieldList.SETRANGE(“Data Type”, NOT fieldList.“Data Type”::Boolean);
IF fieldList.FINDSET THEN BEGIN
WHILE fieldList.NEXT DO BEGIN
MESSAGE(‘Field %1 is mandatory’, fieldList.NAME);
END;
END;
This code uses the FieldRef data type to retrieve the list of fields on the form that are not system fields, are editable, enabled, relevant to the current record, and are marked as mandatory. It then loops through the resulting list and displays a message box for each mandatory field with the field’s name.

You can also modify this code to use the FieldName2Id function to convert the field name to its ID, which is often used in other code in Business Central.

Here is an example link to the Microsoft Dynamics 365 Business Central documentation on this topic: https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-formcontrol-properties-advanced#mandatory

No, there is no direct method on the form level to retrieve the required fields set by the user through the “Personalize” option.