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;
}
0
Answers
change minvalue = 0, for first created.
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;
}
2. Set page field Editable property to MyNumber=0 -> no need to write any code
Thank you.