Dynamics 365 Business Central: How to set the minimum and maximum numeric values ​​for a field

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to set the minimum and maximum numeric values ​​for a field in Business Central.

In some scenarios, we may need to restrict the values ​​​​entered by users, such as only allowing positive or negative numbers. How should this be done in BC?

There are two main simple solutions.

1. Using MinValue Property / MaxValue Property

MinValue Property: Sets the minimum numeric value for a field.

ValueDescription
0Integers
0.0Decimals
January 1, 0Dates
00:00:00Time

MaxValue Property: Sets the maximum numeric value for a field.

ValueDescription
9999Integers
9999.0Decimals
December 31, 9999Dates
23:59:59Time

Applies to:

  • Table field
  • Page Field

Let’s look at a simple example, I created a test field of type Integer with the MinValue property set to 0 and the MaxValue property set to 100.

This means that the range of values ​​that can be entered into this field is greater than or equal to 0 and less than or equal to 100. If it is not within this range, the system will display the following error message. Note that this method cannot exclude equality values; for example, you might only want values ​​greater than 0, not greater than or equal to 0.

The value must be greater than or equal to 0. Value: -1.

The value must be less than or equal to 100. Value: 101.

Test code:

tableextension 50117 VendorExt extends Vendor
{
    fields
    {
        field(50000; "Test Field"; Integer)
        {
            DataClassification = CustomerContent;
            Caption = 'Test Field';
            MinValue = 0;
            MaxValue = 100;
        }
    }
}

pageextension 50117 VendorCardExt extends "Vendor Card"
{
    layout
    {
        addafter(Name)
        {
            field("Test Field"; Rec."Test Field")
            {
                ApplicationArea = All;
            }
        }
    }
}

The field setting is checked during validation. There is another small problem here. Validation occurs only if the field or control value is updated through the UI, for example, if a value is updated on a page or if a field is updated in a table directly. If a field is updated through application code, then the MinValue/MaxValue property is not validated.

2. Using OnValidate (Field) trigger / OnValidate (Page Field) trigger

OnValidate (Field) trigger: Runs when user input is validated.

OnValidate (Page Field) trigger: Runs when a field loses focus after its value has been changed.

If both the table field and page field triggers are defined, then the OnValidate trigger on the table field is run before the OnValidate trigger on the page field.

This is a general method, we can write the logic in it. For example,

Test code:

tableextension 50117 VendorExt extends Vendor
{
    fields
    {
        field(50000; "Test Field"; Integer)
        {
            DataClassification = CustomerContent;
            Caption = 'Test Field';

            trigger OnValidate()
            begin
                if ("Test Field" < 0) or ("Test Field" > 100) then
                    Error('Test Field must be between 0 and 100.');
            end;
        }
    }
}

pageextension 50117 VendorCardExt extends "Vendor Card"
{
    layout
    {
        addafter(Name)
        {
            field("Test Field"; Rec."Test Field")
            {
                ApplicationArea = All;
            }
        }
    }
}

Very simple, give it a try!!!😁

PS: Dynamics 365 Business Central: How to set the initial value (Automatically set default value for field)

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL