Table of Contents
How to make Fields Mandatory in Business Central?
Have you ever had customers insist that specific fields on a page must be filled out? Sometimes, business rules demand it. For example, a customer record needs at least a name and phone number to be valid. But in Business Central, you can create a customer with just a No. field. Sound familiar?
But can users make fields mandatory on their own? Unfortunately, in Business Central, making fields mandatory requires a bit of development magic.
Auto Save Feature in Visual Studio Code ( VS Code )
Here’s a simple guide to making fields mandatory in Business Central. Hope it helps!
Topics Covered for making Field manadatory
Record.TestField Method
This is the kind of first option where method checks if a field’s content matches a given value. If not, it throws an error. Basically by the help of this system validate whether any value exists on that field and if not error message display.
Following Example I will show you how to Vendor master with Name, Address and Mobile Phone No. mandatory.
One or more errors occurred while closing the page. Name must have a value in Vendor: No.=V00020. It cannot be zero or empty.
Sample code to achieve the same is as follows
pageextension 50110 VendorCardExt extends "Vendor Card"
{
trigger OnClosePage()
var
begin
Rec.TestField("name");
Rec.TestField("Address");
Rec.TestField("Mobile Phone No.");
end;
}
Before
System will not ask if we close without entering any value after new number creation
After
System will through error message until we fill the mandatory fields mentioned.
usage syntax for test field is as follows
Record.TestField(Field: Any, Value: Code)
This tests if a field’s content matches a specified value.
- Value: The value to compare with the field. It must match the field’s data type.
- With Value: If included and the field’s content doesn’t match, an error message is shown.
- Without Value: If omitted and the field is zero or blank, an error message is shown.
You add this function inside of OnClosePage()
or OnQueryClosePage()
triggers to enhance this features. But note the difference:
OnClosePage()
: Shows an error but lets you close the page.OnQueryClosePage()
: Shows an error and stops you from closing the page until the mandatory fields are filled.
NotBlank Property in Business Central AL Language
This property makes sure users fill in a selected field or text box. Setting NotBlank
to true on primary fields works well, but it won’t show a red asterisk on the UI.
Sample Error Message
Code to Achieve the same
table 50101 "Navision Planet Sample Table"
{
DataClassification = ToBeClassified;
fields
{
field(1; "Item No."; Code[20])
{
Caption = 'Item No.';
DataClassification = ToBeClassified;
NotBlank = true;
}
field(2; Description; Text[50])
{
Caption = 'Description';
DataClassification = ToBeClassified;
NotBlank = true;
}
field(3; "Price"; Decimal)
{
Caption = 'Price';
DataClassification = ToBeClassified;
}
}
}
- Applies to:
- Table Fields
- Page Fields
ShowMandatory Property
This property marks a field with a red asterisk to indicate it must be filled, but it doesn’t enforce validation. Once filled, the asterisk disappears. It’s purely for show and overrides any marking from the NotBlank Property.
Sample Result
Source code this as follows
pageextension 50110 VendorCardExt extends "Vendor Card"
{
layout
{
modify("Address")
{
ShowMandatory = true;
}
modify("MobilePhoneNo")
{
ShowMandatory = true;
}
}
trigger OnClosePage()
var
begin
Rec.TestField("name");
Rec.TestField("Address");
Rec.TestField("Mobile Phone No.");
end;
}
- Applies to:
- Page Fields
This setting decides if users need to fill in a specific field or text box. The field gets marked with a red star, but it doesn’t check if the value is valid. Once the field is filled, the red star goes away. The ShowMandatory property only affects how the field looks and can replace the red star from the NotBlank Property.
Hope you like this content. Awaiting your valuable feedback. Thanks for reading.
Related Topics