web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Validate fields

(0) ShareShare
ReportReport
Posted on by 555

Hi experts,

In my Business Central cloud extension, I have made my own table and a matching page.

I have 3 fields:

- Code

- Description

- MyNumber (integer)

The user must enter a value in all 3 fields and the field "MyNumber" must be between 1 and 99 and it must be unique.

After the record is created, the user is not allowed to change the field "MyNumber".

I cannot make this simple thing work!!

Can anyone help me, please? You can see my code here:

Table 50107 MyTable
{
    Caption = '..';
    DrillDownPageID = MyPage;
    LookupPageID = MyPage;

    fields
    {
        field(1;"Code";Code[10])
        {
            Caption = 'Code';
            NotBlank = true;
        }
        field(2;"Description";Text[50])
        {
            Caption = 'Desc.';
            NotBlank = true;
        }

        field(3;"MyNumber"; Integer)
        {
            Caption = 'MyNumber';
            NotBlank = true;
            MinValue = 1;
            MaxValue = 99;
            //InitValue = 1;

            trigger OnValidate()
            begin
                validateMyNumber();
            end;
        }
    }

    keys
    {
        key(Key1;"Code", MyNumber)
        {
            Clustered = true;
        }
    }

    fieldgroups
    {
        fieldgroup(DropDown;Code, Description, MyNumber)
        {}
    }

    trigger OnInsert()
    begin
        //validateMyNumber();
    end;

    trigger OnModify()
    begin
        validateMyNumber();
    end;

    local procedure validateMyNumber()
    var
        a: Record MyTable;
    begin
        if (Rec.MyNumber < 1) or
            (Rec.MyNumber > 99) then begin
            Error('Must be between 1 - 99');
        end;

        a.Reset();
        a.SetFilter(Code'<>%1', Rec.Code);
        a.SetFilter(MyNumber'%1', Rec.MyNumber);

        if a.FindFirst() then begin
            Error('This value is already used');
        end;
    end;
}


Page 50108 MyPage
{
    Caption = 'The page';
    PageType = List;
    SourceTable = MyTable;
    ApplicationArea = All;
    UsageCategory = Lists;
    DelayedInsert = true;

    layout
    {
        area(content)
        {
            repeater(Group)
            {
                field("Code";Rec.Code)
                {
                    ApplicationArea = All;
                    ToolTip = '...';
                    //NotBlank = true;
                }

                field(Description;Rec.Description)
                {
                    ApplicationArea = All;
                    ToolTip = '...';
                    //NotBlank = true;
                }                
                field(MyNumber;Rec.MyNumber)
                {
                    ApplicationArea = All;
                    ToolTip = '...';
                    Editable = editNumber;
                    //NotBlank = true;
                }
            }
        }
    }
    trigger OnOpenPage()
    begin
        editNumber := false;
    end;

    trigger OnInsertRecord(BelowxRec: Boolean)Boolean
    begin
        editNumber := true;
    end;

    trigger OnModifyRecord()Boolean
    begin
        editNumber := false;
    end;

    var
        editNumber: Boolean;
}


I have the same question (0)
  • Verified answer
    YUN ZHU Profile Picture
    99,560 Super User 2026 Season 1 on at

    Hi, because "MyNumber" field must be unique, You only need to set it as the key.

    And If you don't want  "MyNumber" field to be modified, you can control it in the OnRename trigger.

    This is not the only way to do it, but I hope it can give you some inspiration.

    pastedimage1630543988925v1.png

    Table 50107 MyTable
    {
        Caption = '..';
        DrillDownPageID = MyPage;
        LookupPageID = MyPage;
    
        fields
        {
            field(1; "MyNumber"; Integer)
            {
                Caption = 'MyNumber';
                NotBlank = true;
                MinValue = 1;
                MaxValue = 99;
            }
            field(2; "Code"; Code[10])
            {
                Caption = 'Code';
                NotBlank = true;
            }
            field(3; "Description"; Text[50])
            {
                Caption = 'Desc.';
                NotBlank = true;
            }
        }
    
        keys
        {
            key(Key1; MyNumber)
            {
                Clustered = true;
            }
        }
    
        fieldgroups
        {
            fieldgroup(DropDown; Code, Description, MyNumber)
            { }
        }
    
        trigger OnRename()
        begin
            Error('the user is not allowed to change the field "MyNumber".');
        end;
    }
    
    
    Page 50108 MyPage
    {
        Caption = 'The page';
        PageType = List;
        SourceTable = MyTable;
        ApplicationArea = All;
        UsageCategory = Lists;
        DelayedInsert = true;
    
        layout
        {
            area(content)
            {
                repeater(Group)
                {
                    field(MyNumber; Rec.MyNumber)
                    {
                        ApplicationArea = All;
                        ToolTip = '...';
                    }
                    field("Code"; Rec.Code)
                    {
                        ApplicationArea = All;
                        ToolTip = '...';
                    }
    
                    field(Description; Rec.Description)
                    {
                        ApplicationArea = All;
                        ToolTip = '...';
                    }
                }
            }
        }
    }

    Thanks.

  • Morten Steengaard Profile Picture
    555 on at

    Hi YUn ZHU,

    Thank you for your reply!

    I think the biggest problem was that I have two fields (Code and MyNumber) that both individually must be unique. Therefore I have decided to have a key with only the field, "Code", in. I think that is the same as what you suggest, only with the other field.

    Thank you very much.

    The result is here and it works fine:

    Table 50107 MyTable

    {

    Caption = '..';

    DrillDownPageID = MyPage;

    LookupPageID = MyPage;

    fields

    {

    field(1;"Code";Code[10])

    {

    Caption = 'Code';

    }

    field(2;"Description";Text[50])

    {

    Caption = 'Desc.';

    }

    field(3;"MyNumber"; Integer)

    {

    Caption = 'MyNumber';

    trigger OnValidate()

    begin

    validateMyNumber();

    end;

    }

    }

    keys

    {

    key(Key1;"Code")

    {

    Clustered = true;

    }

    }

    fieldgroups

    {

    fieldgroup(DropDown;Code, Description, MyNumber)

    {}

    }

    trigger OnInsert()

    begin

    validateMyNumber();

    end;

    trigger OnModify()

    begin

    validateMyNumber();

    end;

    local procedure validateMyNumber()

    var

    a: Record MyTable;

    begin

    if (Rec.MyNumber < 1) or

    (Rec.MyNumber > 99) then begin

    Error('Must be between 1 - 99');

    end;

    a.Reset();

    a.SetFilter(Code, '<>%1', Rec.Code);

    a.SetFilter(MyNumber, '%1', Rec.MyNumber);

    if a.FindFirst() then begin

    Error('This value is already used');

    end;

    end;

    }

    Page 50108 MyPage

    {

    Caption = 'The page';

    PageType = List;

    SourceTable = MyTable;

    ApplicationArea = All;

    UsageCategory = Lists;

    DelayedInsert = true;

    layout

    {

    area(content)

    {

    repeater(Group)

    {

    field("Code";Rec.Code)

    {

    ApplicationArea = All;

    ToolTip = '...';

    }

    field(Description;Rec.Description)

    {

    ApplicationArea = All;

    ToolTip = '...';

    }

    field(MyNumber;Rec.MyNumber)

    {

    ApplicationArea = All;

    ToolTip = '...';

    Editable = editNumber;

    }

    }

    }

    }

    trigger OnOpenPage()

    begin

    editNumber := true;

    end;

    trigger OnAfterGetCurrRecord()

    var

    MyTable: Record MyTable;

    begin

    MyTable.Reset();

    if Rec.RecordId() = MyTable.RecordId() then begin // new record is being created

    editNumber := true;

    end

    else begin

    editNumber := false;

    end;

    end;

    trigger OnInsertRecord(BelowxRec: Boolean): Boolean

    begin

    end;

    trigger OnModifyRecord(): Boolean

    begin

    end;

    var

    editNumber: Boolean;

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,028 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,121 Super User 2026 Season 1

#3
Teagen Boll Profile Picture

Teagen Boll 669 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans