web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Correct the code to adjust item inventory equal to zero

(0) ShareShare
ReportReport
Posted on by 2
Hello,
I try to create an AL procedure that will be used to update Item Inventory equal to zero. I try to use different methods below but they have not worked properly.
Could you please help me to correct the code or show me how to go?
Thanks, a lot.
 
Method 1. custom AL procedure to adjust item inventory equal to zero
 
    procedure UpdateItemInventoryToZero(ItemNo: Code[20])
    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;
 
 
Method 2: Call codeunit 1327 /Adjust Item Inventory/ to adjust item inventory equal to zero
 
    procedure AdjustItemInventoryToZero2(ItemNo: Code[20])
    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;
 
 
Method 3. Utilize codeunit 5895 /Inventory Adjustment/ to create a custom code unit that can be used to adjust item inventory equal to zero
 
codeunit 50147 Cod50147AdjustItemInventory
{
    // 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;
}
 
 
 
 
 
 
 
 
 
 
 
  • Suggested answer
    lionx Profile Picture
    2 on at
    I corrected this myself. Thanks.
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 715 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 452 Super User 2026 Season 2

#3
AndrewThomas81 Profile Picture

AndrewThomas81 371 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans