How do I validate fields?

MortenSteengaard
Member Posts: 144
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;
}
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;
}
0
Answers
-
Since you already put minvalue and maxvalue , you dont have to put other error message
change minvalue = 0, for first created.field(3;"MyNumber"; Integer) { Caption = 'MyNumber'; NotBlank = true; MinValue = 0; MaxValue = 99; //InitValue = 1; trigger OnValidate() begin if xrec.MyNumber <> 0 then error('You are not allowed to modify'); end; }
0 -
Hi julkifli33,
Thank you for your reply. I don't want the user to enter a different number and then afterwards find out that it is not allowed to modify.
I don't want to use MinValue and MaxValue. I cannot remember why. Maybe because if I set them to 10 and 99, and then the user enters 123, then the error message is only that it must not be above 99. I want a precise and user friendly error message.
I think the biggest problem was that I have two fields (Code and MyNumber) that both individually must be unique.
On the Microsoft Community Forum, I got help to solve my problems like this:
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;
}
0 -
1. Integer type has property MinValue and MaxValue -> no need to check explicitly
2. Set page field Editable property to MyNumber=0 -> no need to write any code
0 -
Hi lubost,
Thank you.0 -
Thank you, Juhl.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions