An attempt was made to change an old version of a Purchase Header record

sunnyksunnyk Member Posts: 280
Hello All,
On Codeunit 74, we added a new code in the function - CreateInvLines(.....)
PurchHeader."Your Reference" := PurchRcptHeader."Order No.";
PurchHeader.MODIFY;

and on Codeunit 90, we added the code to assign Your Reference Value to PurchInvHeader.Order No.
But we are getting the following error when posting,

An attempt was made to change an old version of a Purchase Header record. The record should first be reread from the database. This is a programming error.

Identification fields and values:

Document Type='Invoice',No.='XXXXXXXX'

Answers

  • guidorobbenguidorobben Member Posts: 157
    so what is your question?
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Hi,

    This error happens if you try to MODIFY two separate copies of the same record in the code.

    This usually happens if you pass a record to a function, the function parameter does not use VAR, and then you modify the record inside the function, and later on in the code which called the function.

    To show you on an example.
    PurchHeader.FINDFIRST;
    PurchHeader2 := PurchHeader;
    PurchHeader2.MODIFY;
    PurchHeader.MODIFY //<- You will get the same error here
    
    When a record is passed into a function which does not have the rec parameter declared as VAR NAV creates a copy of it. When you modify a record inside the function you effectively create a scenario as in the example above.

    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • sunnyksunnyk Member Posts: 280
    edited 2017-10-11
    But how it is working in version 5 and 2009. The same we copy over from version to version 2016. And there is no change in this codeunit.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Maybe the change is somewhere else. Maybe a MODFIY has been added in CU90, maybe a COMMIT has been taken off, maybe the call to C74 is now in different place.

    I did not post the exact solution. I posted an explanation what is happening and one of possible causes. This is your system, your code modifications, and it is up to you to find the bug. Knowing what and why is happening should make it easier for you.

    Please don't expect us to pinpoint the line of the code causing problem, especially if you post two line of code. I could tell you take the MODIFY off in the code you've posted - it would probably help with the problem you have now, but most likely you would get another problem instead, for example the PurchHeader."Your Reference" field wouldn't be updated anymore.

    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • sunnyksunnyk Member Posts: 280
    Thanks Slawek.
Sign In or Register to comment.