Skip to main content

Notifications

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;
}


  • Morten Steengaard Profile Picture
    555 on at
    RE: Validate fields

    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;

    }

  • Verified answer
    YUN ZHU Profile Picture
    84,346 Super User 2025 Season 1 on at
    RE: Validate fields

    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.

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

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

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 1,452

#2
YUN ZHU Profile Picture

YUN ZHU 1,313 Super User 2025 Season 1

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 1,083 Most Valuable Professional

Featured topics

Product updates

Dynamics 365 release plans