Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Random Inventory

Posted on by 265

Hi all, i'm using Dynamics NAV and my goal is to use Report 790 (Calculate Inventory) to make monthly inventories.

It should generate a random maximum number of products for the user to check.

In the InsertItemJnlLine function I created a variable that counts the number of rows after the journal entry:
...
INSERT (TRUE);
Count Lines: = Count Lines + 1;
...
Then at the end I put the following code:

IF (Count Lines> NumberProducts) AND (NumberProducts <> 0) THEN
  BEGIN
    CurrReport.BREAK;
  END;

Where NumberProducts is the variable that chooses the quantity of products to be inventoried.

But this will only show the number of products I've bound, and does not traverse the table item randomly, this is my first difficulty.
Another difficulty is:
Imagine that 50 products are chosen each month. How do I avoid these 50 products being "chosen" next month.
I thought about creating a variable in the item table, where whenever this report runs, that field is populated with the date and then only apply the filter if that field is empty. Is this acceptable?

*This post is locked for comments

  • Rikarddo Profile Picture
    Rikarddo 265 on at
    RE: Random Inventory

    Thanks Hannes! Works perfectly!!

  • Verified answer
    Hannes Holst Profile Picture
    Hannes Holst 5,767 on at
    RE: Random Inventory

    Hi,

    It's necessary to manage the entries of "QuantityOnHandBuffer".
    See below code:

    8512.Untitled.png

    Best is of course to make a copy of the report.
    Modifying a standard object should be avoided as much as possible.

    LOCAL CalcPhysInvQtyAndInsertItemJnlLine()
    // ++++++++++++++ start new code #1
    QuantityOnHandBuffer.RESET;
    IF NOT ZeroQty THEN // user input
      QuantityOnHandBuffer.SETFILTER(Quantity, '<>0');
    
    // get all items, which have been processed by the report before
    // this will also respect any given filter by the user on the RequestPage
    // ItemBuffer is a temporary variable
    IF QuantityOnHandBuffer.FINDSET THEN
      REPEAT
        IF NOT ItemBuffer.GET(QuantityOnHandBuffer."Item No.") THEN
          BEGIN
            ItemBuffer.INIT;
            ItemBuffer."No." := QuantityOnHandBuffer."Item No.";
            ItemBuffer.INSERT;
          END;
      UNTIL QuantityOnHandBuffer.NEXT = 0;
    
    // now randomize
    MaxStepsPossible := ItemBuffer.COUNT;
    MaxItems := 10; // this could be a new option on the RequestPage
    FOR i := 1 TO MaxItems DO
      BEGIN
        ItemBuffer.FINDFIRST; // always start from first record
        ItemBuffer.NEXT(RANDOM(MaxStepsPossible));
        ItemBuffer.MARK(TRUE);
      END;
    // ---------------- end new code #1
    
    WITH QuantityOnHandBuffer DO BEGIN
      RESET;
    // ++++++++++++++ start new code #2
      // loop through my randomized result of the items which have been processed by the report
      ItemBuffer.MARKEDONLY(TRUE);
      IF ItemBuffer.FINDSET THEN
        REPEAT
          SETRANGE("Item No.", ItemBuffer."No.");
          IF FINDSET THEN
            REPEAT
              MARK(TRUE); // mark the corresponding records in the QuantityOnHandBuffer
            UNTIL NEXT = 0;
        UNTIL ItemBuffer.NEXT = 0;
        SETRANGE("Item No.");
        MARKEDONLY(TRUE); 
    // ---------------- end new code #2
      IF FINDSET THEN BEGIN
        REPEAT
          PosQty := 0;
          NegQty := 0;

    Variables:

    Name Type Comment
    MaxStepsPossible Integer
    MaxItems Integer
    i Integer
    ItemBuffer Record Table 27 "Item"; Temporary=Yes


    Good luck :-)

  • Rikarddo Profile Picture
    Rikarddo 265 on at
    RE: Random Inventory

    Hannes thanks for the tips.

    I've created a Report to test it , and it works like a charm. Now my difficulty is to pass that maxitems to the JournalLine.

    This is the normal behaviour, where creates the lines in the journal.

    Item - OnPostDataItem()

    CalcPhysInvQtyAndInsertItemJnlLine;

    How to pass the random items?

  • Suggested answer
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 on at
    RE: Random Inventory

    You need to use the RANDOMIZE and RANDOM functions to loop a random item, get the Item Count and use those functions to get a random number less than item count and for your second issue yes, create a date field and stamp the date when you have processed such way you can filter the items based on the date.

  • Hannes Holst Profile Picture
    Hannes Holst 5,767 on at
    RE: Random Inventory

    As I understand, you want to select random records from a Table.

    You can use the following code as a start:

    MaxStepsPossible := ItemRec.COUNT;
    MaxItems := 50;
    FOR i := 1 TO MaxItems DO
      BEGIN
        ItemRec.FINDFIRST; // always start from first record
        ItemRec.NEXT(RANDOM(MaxStepsPossible));
        CreateJournalLine(ItemRec); // add here your function to create the Journal Line
      END;


    You can test the overall usability of the function with a slightly modified version:

    MaxStepsPossible := ItemRec.COUNT;
    MaxItems := 10;
    FOR i := 1 TO MaxItems DO
      BEGIN
        ItemRec.FINDFIRST; // always start from first record
        ItemRec.NEXT(RANDOM(MaxStepsPossible));
        RandomItems += ItemRec."No." + '\'; //RandomItems is a Text-variable
      END;
    
    MESSAGE(RandomItems);
    


    RandomItems will contain different values each time :-)

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 Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans