Could you please help me to correct the code or show me how to go?
var
Item: Record Item;
ItemJnlLine: Record /Item Journal Line/;
ItemJnlTemp: Record /Item Journal Template/;
ItemJnlBatch: Record /Item Journal Batch/;
ItemLedgerEntry: Record /Item Ledger Entry/;
begin
// Check if the new item exists
if not Item.Get(ItemNo) then
Error('Item %1 does not exist.', ItemNo);
// Create a filter for the record item to adjust inventory
Item.SETRANGE(/No./, ItemNo);
if Item.Inventory = 0 then
exit;
// Set the name of the item journal batch
IF NOT (ItemJnlTemp.GET('ITEM')) THEN
CLEAR(ItemJnlTemp);
IF NOT (ItemJnlBatch.GET(ItemJnlTemp.Name, 'UPDATENOIN')) THEN
Error('Item Journal Batch %1 does not exist.', 'UPDATENOIN');
// Reset & setup for the item journal line
ItemJnlLine.Reset();
ItemJnlLine.SetRange(/Journal Template Name/, ItemJnlTemp.Name);
ItemJnlLine.SetRange(/Journal Batch Name/, ItemJnlBatch.Name);
if not ItemJnlLine.FindLast() then
LineNo := 10000
else
LineNo := ItemJnlLine./Line No./ + 10000;
// Filter and Calculate item inventory for all locations in item Ledger entry
ItemLedgerEntry.SetRange(/Item No./, ItemNo);
ItemLedgerEntry.SetFilter(/Remaining Quantity/, '>0');
//ItemLedgerEntry.CalcSums(/Location Code/, /Remaining Quantity/);
// Findset and Loop
IF ItemLedgerEntry.FindSet(true, false) THEN BEGIN
REPEAT
// Initialize a new item journal line
Clear(ItemJnlLine);
ItemJnlLine.Init();
ItemJnlLine./Journal Template Name/ := 'ITEM';
ItemJnlLine.VALIDATE(/Journal Template Name/);
ItemJnlLine./Journal Batch Name/ := 'UPDATENOIN';
ItemJnlLine.VALIDATE(/Journal Batch Name/);
ItemJnlLine./Document No./ := 'UPDATENOIN';
ItemJnlLine.VALIDATE(/Document No./);
ItemJnlLine./Line No./ := LineNo;
ItemJnlLine./Item No./ := ItemNo;
ItemJnlLine./Entry Type/ := ItemJnlLine./Entry Type/::/Negative Adjmt./;
ItemJnlLine.VALIDATE(/Entry Type/);
ItemJnlLine./Location Code/ := ItemLedgerEntry./Location Code/;
ItemJnlLine.VALIDATE(/Location Code/);
ItemJnlLine.Quantity := ItemLedgerEntry./Remaining Quantity/;
ItemJnlLine.VALIDATE(Quantity);
ItemJnlLine./Lot No./ := ItemLedgerEntry./Lot No./;
ItemJnlLine.VALIDATE(/Lot No./);
// Insert the item journal line
ItemJnlLine.Insert(True);
UNTIL ItemLedgerEntry.Next() = 0;
// Post item journal line: post all lines
CODEUNIT.Run(CODEUNIT::/Item Jnl.-Post Batch/, ItemJnlLine)
END
end;
var
RecItem: Record Item;
AdjustItemInventoryCodeunit: Codeunit /Adjust Item Inventory/;
ItemLedgerEntry: Record /Item Ledger Entry/;
begin
// Check if the item exists
if not RecItem.Get(ItemNo) then
Error('Item %1 does not exist.', ItemNo);
// Create a filter for the record item to adjust
RecItem.SETRANGE(/No./, ItemNo);
// Calculate total inventory of record item in all locations
ItemLedgerEntry.SetRange(/Item No./, ItemNo);
ItemLedgerEntry.SetFilter(/Remaining Quantity/, '>0');
ItemLedgerEntry.CalcSums(/Remaining Quantity/);
NewInventory := ItemLedgerEntry./Remaining Quantity/;
// Adjust the item inventory
AdjustItemInventoryCodeunit.PostAdjustmentToItemLedger(RecItem, -RecItem.Inventory);
end;
{
// Trigger Codeunit
trigger OnRun()
begin
// Call the procedure with a sample item number
//AdjustItemInventoryToZero('ITEM001');
end;
procedure AdjustItemInventoryToZero(ItemNo: Code[20])
var
FilterItem: Record Item;
InventoryAdjmtCodeunit: Codeunit /Inventory Adjustment/;
begin
// Check if the item exists
if not FilterItem.Get(ItemNo) then
Error('Item %1 does not exist.', ItemNo);
// Create a filter for the record item to adjust
FilterItem.SetRange(/No./, ItemNo); // FilterItem.SetFilter(/No./, ItemNo);
// Initialize the Inventory Adjustment codeunit
InventoryAdjmtCodeunit.SetProperties(false, false);
InventoryAdjmtCodeunit.SetFilterItem(FilterItem);
InventoryAdjmtCodeunit.MakeMultiLevelAdjmt();
end;
}