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.
![](https://yzhums.com/wp-content/uploads/2021/03/image-385-1024x459.png)
Table: Salesperson/Purchaser (13)
![](https://yzhums.com/wp-content/uploads/2021/03/image-388-1024x494.png)
![](https://yzhums.com/wp-content/uploads/2021/03/image-389.png)
So, you can use Record.Relation Method to get the table number (13) of Salesperson/Purchaser.
![](https://yzhums.com/wp-content/uploads/2021/03/image-393.png)
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’
![](https://yzhums.com/wp-content/uploads/2021/03/image-394-1024x428.png)
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)
![](https://yzhums.com/wp-content/uploads/2021/03/image-391.png)
No relationship has been associated with the field Name in Customer.
Page My Company 02, Sales, Customers has to close.
![](https://yzhums.com/wp-content/uploads/2021/03/image-390-1024x361.png)
2. The table ID you get can be used in RecordRef Data Type.
A simple example: Change the SalesPerson name to ‘New Name’.
![](https://yzhums.com/wp-content/uploads/2021/03/image-395.png)
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
コメント