I currently investigated the following NAV Error reported in a support incident:
An attempt was made to change an old version of a Customer record. The record should first be reread from the database. This is a programming error.
Identification fields and values:
This NAV error can appear on any record, I forced it on the Customer record in order to understand why it appears, what does it mean and how to fix it.
Create a new test Codeunit and add the following variables and code:
OnRun() Cust.FindFirst; Cust.Name := 'AAA'; Cust2.FindFirst; Cust2.Name := 'BBB'; Cust2.MODIFY; Cust.MODIFY;
So, what am I doing here ?
On the first Cust variable I am retrieving from the database the first customer record. In my case, on the Test Chronus Database is customer ‘01121212’. I change the initial Name ‘Spotsmeyer’s Furnishings’ to ‘AAA’, but I don’t modify the record into the database too.
On the second Cust2 variable I retrieve the same record from the database, change the Name to ‘BBB’ and modify the record into the database with this value.
In the end I want to modify the name of the Cust variable (‘AAA’) and at this moment the system throws the error because the record was already changed into the database by variable Cust2 and it was not reread.
If you think more this error is actually correct because the system (and also you) does not know which of the two values should be the final one ? In this case it is a programming error 🙂
I saw in this post the solution to use FIND before the Cust.Modify in order to retrieve the last version from the database (the one modified by variable Cust2). This indeed solves the error, but there are 2 problems:
- You are not sure that the Name of the Customer is the correct one.
- More important: if before you modify Cust2 you change other fields on the Cust variable as in the bellow example, all the modifications that you tried to do on the Cust variable will be lost. When you use FIND (like in my below example), you take the last version of the ‘modified’ record (this does not contain the modifications you tried to do on the Cust variable !!!) and then Modify (in this example you actually modify nothing 🙂 )
OnRun() Cust.FindFirst; //those 3 modifications are lost because of the FIND ! Cust.Name := 'AAA'; Cust."Location Code" := 'ZILVER'; Cust.Contact := 'Andrei'; Cust2.FindFirst; Cust2.Name := 'BBB'; Cust2.MODIFY; Cust.FIND; //find added ! Only the Name is changed to 'BBB' Cust.MODIFY;
So, please be careful when using FIND not to loose other information ! The better solution to solve this Nav Error is to move the Cust.Modify before the Cust2.Modify. In this case the Name will be ‘BBB’ but the ‘Location Code’ and ‘Contact’ will be changed according to modifications done on Cust variable.