Take a look at this code - can you spot the problem?
The intention is clear - and the code looks OK at a glance. But no. This code allows order lines to be inserted without the header being updated and miscommunicates to the user that the order was not updated.
Here is what happens when a MyOrderLine record is inserted on a cancelled order:
- We find the order header - line 22
- We set the LastUpdate field - line 23
- We check if the order is valid - line 25 - if not we just continue silently.
In other words, we allow the line to be inserted on a cancelled order. This is data corruption.
The example above is simplified to highlight the problem. There are many variants, but the pattern to look for is the same.
The fix is simple
Whenever you need to ensure a record is valid, make sure to throw an exception when it is not.
Note: There may be cases where the validation is superflous - in those cases, don't call validateWrite.
This applies to both validateWrite() and validateDelete() when called from X++ code.
Now what?
Go through all your code where you call validateWrite() and validateDelete(), make sure an exception thrown.
Please post your experiences as comments. How many incidents did you find? How horrible were the data corruptions you prevented?
THIS POST IS PROVIDED AS-IS; AND CONFERS NO RIGHTS.
*This post is locked for comments