Skip to main content

Notifications

Announcements

No record found.

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
    lionx 2 on at
    Correct the code to adjust item inventory equal to zero
    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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans