Dynamics 365 Business Central: How to make a field mandatory

Dynamics 365 Business Central

Hi, Readers.
Have you ever come across some customers who want certain fields on the page to be mandatory?
In the case of some business logic, certain fields must be populated in order for the data to be valid. For example, a customer record must have at least a name and phone number. Since it is not a primary key, Customers in Business Central only need No. to be created. So I think this is a very common requirement.

But can a user make fields mandatory at will? Unfortunately, until now in Business Central to make fields mandatory needs development.

In this post, I will share some tips to make a field mandatory in Business Central. Hope this will help.

Record.TestField Method

Tests whether the contents of a field match a given value.
The value that you want to compare to Field. The data type of this parameter must match the data type of Field. If you include this optional parameter and the contents of Field do not match, then an error message is displayed. If you omit this parameter and the contents of Field is zero or blank (empty string), then an error message is displayed.

For example:

Name must have a value in Customer: No.=C00060. It cannot be zero or empty.

Using OnClosePage() trigger or OnQueryClosePage() trigger on the page is a good idea. But these two triggers are different.

OnClosePage() trigger: Error message will shown, but the page can be closed.

OnQueryClosePage() trigger: Error message will shown, and the page can not be closed until you enter the value of the mandatory fields.

Find out more about Record.TestField Method from Microsoft Docs.

NotBlank Property

Sets a value that specifies whether users must enter a value in the selected field or text box.

You can set the NotBlankproperty to true on primary fields in a table. Setting the NotBlank property to true on other fields in a table does not cause a red star marking on the UI.
Applies to:
・Table Fields
・Page Fields

For Eample:

This property cannot be customized in TableExtension and PageExtension.

If the NotBlank property is used on other fields than primary fields, the behavior is as follows:
・If the field is blank, and never entered, the page can be closed.
・If the field has been entered, and the user tries to enter a blank value, a validation error occurs.

Find out more about NotBlank Property from Microsoft Docs.

ShowMandatory Property

Sets a value that specifies whether users must enter a value in the selected field or text box. The field is marked on the page with a red asterisk and does not enforce any validation. Once the field is filled, the red asterisk disappears. The ShowMandatory property only controls the UI and overrides any asterisk marking of the NotBlank Property.
So you can actually ignore it.

The ShowMandatory property can be specified as truefalse, or as an expression.
Applies to:
・Page Fields

This property cannot be customized in PageExtension.

For example:

Find out more about ShowMandatory Property from Microsoft Docs.

Hope this will help.

Thanks for your reading.

ZHU

コメント

Copied title and URL