Getting primary key in code

EvREvR 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

Best Answers

Answers

  • EvREvR Member Posts: 178
    Awesome, thanks! :)
  • vaprogvaprog Member Posts: 1,144
    edited 2017-05-10
    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
    KeyRef := RecRef.KEYINDEX(RecRef.CURRENTKEYINDEX);
    

  • NavNabNavNab Member Posts: 181
    vaprog 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 key ;)
    EvR wrote: »
    Is it possible to get a table's primary key fields in code?

Sign In or Register to comment.