Tricks to find the Last PK

navuser1navuser1 Member Posts: 1,329

The following Standard Code in NAV is occasionally used to find the LAST No. (read Last PK) of the targeted* table.

* in where the programme is going to INSERT the records.
IF ItemLedgEntryNo = 0 THEN BEGIN
    IF GlobalItemLedgEntry.FINDLAST THEN
      ItemLedgEntryNo := GlobalItemLedgEntry."Entry No.";

May I know why does NAV use the First Line --"IF ItemLedgEntryNo = 0" in the beginning ?
What will be the problem if the mentioned is removed ?

Kindly reply.

Now or Never


  • Options
    SonGoten13SonGoten13 Member Posts: 44
    Well the PK could be populated somewhere before, so the Code is not necessary. It is always good to avoide LOCKTABLE.
    If there is a problem if you remove it depends on the exact situation, and if it is somewhere in the posting process like Cu 22 it is hard to tell.
    Anyway i see no reason to remove it in first place, bad idea imo.
  • Options
    KishormKishorm Member Posts: 921
    The reason for that line is performance, basically when posting multiple journal lines you only need to check for the last posted entry once and then you can simply increment for each journal line being posted. If you remove that line (and the corresponding END) then you won't break anything - it will still work correctly but it will just be a bit slower.
  • Options
    navuser1navuser1 Member Posts: 1,329
    Thanks for your reply.

    Apart from your (Technical) opinions I have come to know the below function part also.

    A LOCKTABLE in SQL Server does not lock anything. It's used to set the SET TRANSACTION ISOLATION LEVEL(SERIALIZABLE).

    When we post a (Batch Posting) Item Journal Lines/Gen. Journal Lines, we all know that the posted entries(Item Ledger Entry & G/L Entry) are (should be) in Sequence in PK.

    If we remove the mentioned line from the code then there is a possibility for the system to have a GAP in PK sequence.
    Now or Never
Sign In or Register to comment.