Hi, Readers.
Today I would like to share another mini tip of Business Central. How to Determines the table relationship of a given field. In other words, how to get the table number of the field where the TableRelation is set.
This time we will use Record.Relation Method.
Syntax:
TableNumber := Record.Relation(Field: Any)
Parameters:
Record Type: Record An instance of the Record data type.
Field
Type: Any
The field for which you want to find the table relationship.
Return Value:
TableNumber Type: Integer
For example:
SalesPerson Code on the Customer Card page.
Table: Salesperson/Purchaser (13)
So, you can use Record.Relation Method to get the table number (13) of Salesperson/Purchaser.
Sample Code:
pageextension 50115 CustomerListExt extends "Customer Card"
{
trigger OnOpenPage()
var
TableNumber: Integer;
begin
TableNumber := Rec.Relation("Salesperson Code");
Message('Table Number is ''%1''', TableNumber);
end;
}
Table Number is ’13’
Note:
1. Although the field in Record.Relation can add any field and compile without any errors, please note that if you add a field that does not contain TableRelation property, there will be a runtime error.
For example: Rec.Relation(Name)
No relationship has been associated with the field Name in Customer.
Page My Company 02, Sales, Customers has to close.
2. The table ID you get can be used in RecordRef Data Type.
A simple example: Change the SalesPerson name to ‘New Name’.
Source Code:
pageextension 50115 CustomerListExt extends "Customer Card"
{
trigger OnOpenPage()
var
TableNumber: Integer;
RecRef: RecordRef;
FldRef: FieldRef;
begin
TableNumber := Rec.Relation("Salesperson Code");
Message('Table Number is ''%1''', TableNumber);
RecRef.Open(TableNumber);
FldRef := RecRef.Field(1);
if FldRef.Active then begin
FldRef.Value(Rec."Salesperson Code");
FldRef := RecRef.Field(2);
FldRef.Value('New Name');
RecRef.Modify();
Message('SalesPerson name has changed to ''%1''.', FldRef.Value);
end;
end;
}
Test Video:
END
Hope this will help.
Thanks for reading.
ZHU
コメント