Getting primary key in code

EvR
Member Posts: 178
Hi all,
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.
Gr, Erik
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.
Gr, Erik
0
Best Answers
-
Hi Erik,
Check the CURRENTKEY Function (RecordRef).
Regards,
Diwakar6 -
Hi Erik,
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.
7
Answers
-
Hi Erik,
Check the CURRENTKEY Function (RecordRef).
Regards,
Diwakar6 -
Awesome, thanks!0
-
Hi Erik,
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.
7 -
nabil.bamoh@hotmail.com wrote: »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.CURRENTKEYINDEXKeyRef := RecRef.KEYINDEX(RecRef.CURRENTKEYINDEX);
0 -
nabil.bamoh@hotmail.com wrote: »CURRENTKEY will not give you exact answer if "SETCURRENTKEY" was used before... (somewhere in the code)
Really? Can you give an example for this?
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 keyIs it possible to get a table's primary key fields in code?
1
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions