Is it possible to get a table's primary key fields in code?
I've looked at the virtual Field table and FieldRef but I can't find anything that points to the PK.
CURRENTKEY will not give you exact answer if "SETCURRENTKEY" was used before... (somewhere in the code)
Instead, you can use virtual table "Key" and filter to key number 1, or simply do a GET ==> Key.GET(TableNo, 1)
Example:
Key.GET(36, 1);
MESSAGE('%1', Key.Key); // Will show "Document Type,No."
On another note, here is a function that tells you if a field is part of the primary key:
IsPrimaryKey(_TableNo : Integer;_FieldNo : Integer) : Boolean
RecRef.OPEN(_TableNo);
FOR i := 1 TO RecRef.KEYINDEX(1).FIELDCOUNT DO
IF RecRef.KEYINDEX(1).FIELDINDEX(i).NUMBER = _FieldNo THEN
EXIT(TRUE);
CURRENTKEY will not give you exact answer if "SETCURRENTKEY" was used before... (somewhere in the code)
Instead, you can use virtual table "Key" and filter to key number 1, or simply do a GET ==> Key.GET(TableNo, 1)
Example:
Key.GET(36, 1);
MESSAGE('%1', Key.Key); // Will show "Document Type,No."
On another note, here is a function that tells you if a field is part of the primary key:
IsPrimaryKey(_TableNo : Integer;_FieldNo : Integer) : Boolean
RecRef.OPEN(_TableNo);
FOR i := 1 TO RecRef.KEYINDEX(1).FIELDCOUNT DO
IF RecRef.KEYINDEX(1).FIELDINDEX(i).NUMBER = _FieldNo THEN
EXIT(TRUE);
CURRENTKEY will not give you exact answer if "SETCURRENTKEY" was used before... (somewhere in the code)
Really? Can you give an example for this?
CURRENTKEY returns a string with field captions of the fields in the key NAV selected, when you executed the SETCURRENTKEY statement. Note that there are rules, how NAV selects a key from the fields given as parameters to SETCURRENTKEY. The key selected does not necessarily contain those fields only.
For display, CURRENTKEY is suitable, for anything more technical, use either the Key virtual table or a KeyRef.
As a side note: to find the current key (rather than the primary key), use RecordRef.CURRENTKEYINDEX
Answers
Check the CURRENTKEY Function (RecordRef).
Regards,
Diwakar
CURRENTKEY will not give you exact answer if "SETCURRENTKEY" was used before... (somewhere in the code)
Instead, you can use virtual table "Key" and filter to key number 1, or simply do a GET ==> Key.GET(TableNo, 1)
Example:
Key.GET(36, 1);
MESSAGE('%1', Key.Key); // Will show "Document Type,No."
On another note, here is a function that tells you if a field is part of the primary key:
IsPrimaryKey(_TableNo : Integer;_FieldNo : Integer) : Boolean
RecRef.OPEN(_TableNo);
FOR i := 1 TO RecRef.KEYINDEX(1).FIELDCOUNT DO
IF RecRef.KEYINDEX(1).FIELDINDEX(i).NUMBER = _FieldNo THEN
EXIT(TRUE);
I hope this will help.
Really? Can you give an example for this?
CURRENTKEY returns a string with field captions of the fields in the key NAV selected, when you executed the SETCURRENTKEY statement. Note that there are rules, how NAV selects a key from the fields given as parameters to SETCURRENTKEY. The key selected does not necessarily contain those fields only.
For display, CURRENTKEY is suitable, for anything more technical, use either the Key virtual table or a KeyRef.
As a side note: to find the current key (rather than the primary key), use RecordRef.CURRENTKEYINDEX
Here is the example you asked:
CLEAR(ItemLedgerEntry);
MESSAGE(ItemLedgerEntry.CURRENTKEY);
ItemLedgerEntry.SETCURRENTKEY("Document No.","Document Type","Document Line No.");
MESSAGE(ItemLedgerEntry.CURRENTKEY);
Note that Eric (EvR) is asking for primary key