Dynamics 365 Business Central: How to quickly get the primary key values of the record (Record.GetPosition Method and RecordId Method)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to quickly get the primary key values of the record in Business Central.

As you might know, we can use Record.Get() Method to get a record based on values stored in primary key fields. But on the other hand, we have already got the record, how to find out the values in the in primary key fields? At this time we don’t even know which fields are the primary key. Is there any easy way to do it?

Yes, in this post I will share two ways.

1. Using Record.GetPosition([Boolean]) Method

Record.GetPosition([Boolean]): Gets a string that contains the primary key of the current record.

Syntax: String := Record.GetPosition([UseNames: Boolean])

Parameters:
Record: An instance of the Record data type.
[Optional] UseNames: Indicates whether a reference to the field caption or the field number should be returned. If set to true (default value) or empty, then the returned string contains references to field captions in the table with which the record is associated. If a field doesn’t have a caption, then the name is returned. If the parameter is set to false, then field numbers are used instead.

Let’s see a simple example.

Rec.GetPosition(true):

Result: Document Type=CONST(Order),No.=CONST(101001)

Rec.GetPosition(false):

Result: Field1=0(1),Field3=0(101001)

2. Using RecordId Method

RecordId: Gets the RecordId of the record that is currently selected in the table. If no table is selected, an error is generated.

Syntax: RecordId := Record.RecordId;

You can show strings that include RecordId in the user interface by using the Format Method (Code, Text).

“RecordId” contains the table number and primary key value of a record.
Let’s see a simple example.

Result: Sales Header: Order,101001

PS:
1. You can also use RecordId.TableNo() to get the table number of the table that is identified by RecordID.

2. You can find a example about RecordId Data Type in table 454 “Approval Entry”

3. You cannot use the Get method to retrieve a record in a table by its primary key value if the primary key field in the table has the data type RecordID. This is because RecordId already is the primary key itself and not one of the fields that forms it, as the method expects. In this case, you can retrieve the record by using the SetRange Method.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL