IF AvgCostAdjmt.FIND('+') THEN;

In Report 795 there is a line of code that I do not understand:

IF AvgCostAdjmt.FIND(’+’) THEN;

What is the function here?

Table 5804 Average Cost Adjustment is empty.

Looks to me like that report wants to use the last record on that table. Programming it that way it ‘finds’ an empty record instead of erroring out if it doesn’t find any records. See if you can find other intances of the AvgCostAdjmt variable, and you should find what is used. If you can’t find any other instance, I’d say you can safely rem out that line of code.

That AvgCostAdjmtEntryPoint.FIND(’+’) it’s unnecessary. It’s safe to remove it.

It guess that previously that IF THEN ELSE had an EXIT statement

Hi Colin,

actually just looking closer, I see that 795 is the Adjust Inventory Cost routine. So be carefull making any changes.

First question: are you asking because there is a problem you want to fix, or is this just out of curiosity? Second question Which version of Navision, since this routine is different in most country versions of Navision, and every version changes it, and more service packs change it.

But back to your specific question. Firstly that code is badly written, and should be changed to a FINDLAST.

The code is there specifically for SQL. And the code only makes sense if you include the whole code snippet, not just a part of it, the full code snippet is.


Native DB uses optomistic concurrency, so it does not need to explicitly lock all records in the database. SQL uses ROW level locking, and thus if you need to force a Table lock, then you first tell SQL to lock the first ROW (record), and then tell it to find the last record. Finding the last record then forces a ROW lock on every record in the table, thus the equiivilent of a table lock.

If you didn’t do this, then it would be possible for someone to modify some (though not all) of the records involved in the adjustment routine, and you could get an inconsistent result.

Hi David,

What I am doing is finding out how the cost adjustment works as the client wants to update the posted sales lines each time the purchase invoice is posted.

I saw the line of code and just did not understand it.Your explanation is excellent, thank you.

They are running SQL. The version is 3.1 and the cost adjustment process has been modified to update the posted sales lines as well as the Customer Ledger Entry so that the reporting for margins will be correct when they run the reports from that table.

The real problem is the time it takes the batch program to run, they have over 4GB database and it takes several hours to complete, that is something else I am looking into to see if I can speed the process. If I can, then they can run the update each night if they want.

Best regards,


I checked code again, and David you are 100% right in your comments. [:D]
Without that line in certain conditions you could have deadlocks if users were posting at the same time has the adjust cost. I assumed that locks also were made in codeunits and I has wrong.

I don’t think you could replace it with FINDLAST. If I’m not wrong this time, SQL locks records or pages has records are begin read. When you replace it with FINDLAST, Navision will convert it to SELECT TOP 1 * DESC. This statement will probably will get only a record and only last record will be lock. I’m not 100% sure about last comment, probably some else could comment it.

To lock a table in SQL, it is not necessary to lock all the records in the table.

Just lock 1 record and if all other objects that want to change something want to lock the table, lock the same record, you have your ‘simulated’ tablelock.

Codeunit 22 (and others) do just that: