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

sunnyksunnyk Posts: 263Member
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 Posts: 128Member
    so what is your question?
  • Slawek_GuzekSlawek_Guzek Posts: 960Member
    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
  • sunnyksunnyk Posts: 263Member
    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 Posts: 960Member
    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
  • sunnyksunnyk Posts: 263Member
    Thanks Slawek.
Sign In or Register to comment.