Block the posting if one line has negative inventory

Hi everyone,
I want to do some control in the sales orders posting.
Let’s say I have an order with 5 lines, and the 3rd line presents a negative inventory.
I want to be able to block the posting of the whole sales order (no shipment header or lines are generated).
How can I do that?
I tried to do some workaround in CU 22 in the function InsertItemLedgEntry

//this is standard code
IF “Job No.” <> ‘’ THEN BEGIN
ItemLedgEntry.“Job No.” := “Job No.”;
ItemLedgEntry.“Job Task No.” := “Job Task No.”;
//my code starts here
IF ((ItemLedgEntry.“Entry Type” IN
[ItemLedgEntry.“Entry Type”::“Negative Adjmt.”,
ItemLedgEntry.“Entry Type”::Sale,ItemLedgEntry.“Entry Type”::Purchase
]) AND (ItemLedgEntry.Quantity <0) )THEN
lItem.GET(ItemLedgEntry.“Item No.”);
lItem.SETFILTER(“Location Filter”,ItemLedgEntry.“Location Code”);
IF (lItem.Inventory < ABS(ItemLedgEntry.Quantity)) THEN
ERROR(Error0001, lItem.“No.”,ABS(ItemLedgEntry.Quantity),lItem.Inventory);
//my code ends here
//this is standard code
InsertItemReg(ItemLedgEntry.“Entry No.”,0,0,0);

With this code, I get the error message and the posting stops, but when I look at table 32 (Item Ledger entry), I find that the two lines (with no negative inventory) have been posted.
How shall I do to prevent the posting of the whole order if there is one or more lines with negative inventory?
Thanks in advance :)

But if your code has been changed, and there is a COMMIT added, your mod will not work.

I would put my code into the end of function InitItemLedgerEntry.
Doing it here, there is no need to check the Entry Type, only “Quantity < 0”.