Tuesday, May 17, 2016

Code Behind "How to Prevent Negative Inventory" - Versions After 2013

As mention in my previous blog post (How to Prevent Negative Inventory - Versions After 2013) Microsoft has added a option to prevent negative inventory in Microsoft Dynamics NAV. To find the code behind the validation I have import all the object files to "Statical Prism". Then I search for the Inventory Setup table which hold the field "Prevent Negative Inventory". Once the table is been identified in the field level I look for all the field usage. 

Here we go...
Code is in the Item Table under the function of PreventNegativeInventory
 PreventNegativeInventory() : Boolean  
 CASE "Prevent Negative Inventory" OF  
  "Prevent Negative Inventory"::Yes:  
   EXIT(TRUE);  
  "Prevent Negative Inventory"::No:  
   EXIT(FALSE);  
  "Prevent Negative Inventory"::Default:  
   BEGIN  
    InventorySetup.GET;  
    EXIT(InventorySetup."Prevent Negative Inventory");  
   END;  
 END;  

But this is not the complete one, we need to find other places where this function is been called. So I went back to "Statical Prism" and search for the PreventNegativeInventory function usage.

As shown in the above picture  PreventNegativeInventory function is called from Item Ledger Entry Table
 VerifyOnInventory()  
 IF NOT Open THEN  
  EXIT;  
 IF Quantity >= 0 THEN  
  EXIT;  
 CASE "Entry Type" OF  
  "Entry Type"::Consumption,"Entry Type"::"Assembly Consumption","Entry Type"::Transfer:  
   ERROR(IsNotOnInventoryErr,"Item No.");  
  ELSE BEGIN  
   Item.GET("Item No.");  
   IF Item.PreventNegativeInventory THEN  
    ERROR(IsNotOnInventoryErr,"Item No.");  
  END;  
 END;  

As in the code this is not the complete code, to find out the missing part open up the "Statical Prism" and search for VerifyOnInventory function usage. 

There you go, In the "Item Jnl.-Post Line" Code Unit under InsertItemLedgEntry function it calls for VerifyOnInventory function and it trigger the entire process. Following is the function code. 
 LOCAL InsertItemLedgEntry(VAR ItemLedgEntry : Record "Item Ledger Entry";TransferItem : Boolean)  
 WITH ItemJnlLine DO BEGIN  
  IF ItemLedgEntry.Open THEN BEGIN  
   ItemLedgEntry.VerifyOnInventory;  
   IF NOT (("Document Type" IN ["Document Type"::"Purchase Return Shipment","Document Type"::"Purchase Receipt"]) AND  
       ("Job No." <> ''))  
   THEN  
    IF (ItemLedgEntry.Quantity < 0) AND  
      (ItemTrackingCode."SN Specific Tracking" OR ItemTrackingCode."Lot Specific Tracking")  
    THEN  
     ERROR(Text018,"Serial No.","Lot No.","Item No.","Variant Code");  

Hope you got the code behind the prevent negative inventory option in Microsoft Dynamics NAV. 


Please provide your feedback with a comment. 
Thank you and Regards,
Tharanga Chandrasekara

2 comments:

  1. I found this article easy to understand and very helpful. Can’t wait to see the other posts. Thank you for sharing!

    ReplyDelete