X++ Code to find OnHand Stock for Item

In Dynamics Ax , use InventOnHand class for finding the stock of an item as shownbelow



static void findOnHand(Args _args)
{

InventDim inventDim;
InventDimParm inventDimParm;
Itemid itemid;
InventOnHand inventOnHand = new InventOnHand();
;

// take a sample item for testing
itemid = "20 MM RMC";

// take a combination of dimension , against which you want to find the stock
inventDim.InventLocationId = "GW";

//Set the flag for the selected dimensions as active.
inventDimParm.initFromInventDim(inventDim);

//initialize the inventonhand with item,dimension and dim paramter

inventOnHand.parmItemId(itemid);
inventOnHand.parmInventDim(inventDim);
inventOnHand.parmInventDimParm(inventDimParm);

// Retrieve the onhand info
info(strfmt("Available Physical: %1",
inventOnhand.availPhysical()));
info(strfmt("On order: %1",inventOnhand.onOrder()));

}

2 comments:

  1. Thank you for the clear explanation.
    But your code seems to contain a small error though.

    The sequence of initialization of inventOnHand must be changed.
    It should be:

    //initialize the inventonhand with item,dimension and dim paramter
    inventOnHand.parmInventDimParm(inventDimParm);
    inventOnHand.parmInventDim(inventDim);
    inventOnHand.parmItemId(itemid);

    You have to do inventOnHand.parmItemId(itemid) last,
    because this method sets a flag on the local inventDimParm buffer.

    In you case, you build an inventDimParm on beforehand, that doesn't contain the itemId flag (so it's FALSE).
    Your call to inventOnHand.parmItemId(itemid) sets this flag to TRUE.

    See:
    void parmItemId(ItemId _itemId)
    {
    ;
    itemId = _itemId;
    inventDimParm.ItemIdFlag = true;
    }

    But the itemId flag gets set to FALSE again when you call inventOnHand.parmInventDimParm(inventDimParm)
    because the inventDimParm you built on beforehand has the itemId flag set to FALSE.


    As an alternative, you could use the newParameter static on inventOnHand:
    (then you don't have to worry about the issue of the itemId flag)

    static void findOnHand(Args _args)
    {
    InventDim inventDim;
    InventDimParm inventDimParm;
    Itemid itemid;
    InventOnHand inventOnHand = new InventOnHand();
    ;

    // take a sample item for testing
    itemid = "20 MM RMC";

    // take a combination of dimension , against which you want to find the stock
    inventDim.InventLocationId = "GW";

    //Set the flag for the selected dimensions as active.
    inventDimParm.initFromInventDim(inventDim);

    //initialize the inventonhand with item,dimension and dim paramter
    inventOnHand = InventOnHand::newParameters(itemid, inventDim, inventDimParm);

    // Retrieve the onhand info
    info(strfmt("Available Physical: %1", inventOnhand.availPhysical()));
    info(strfmt("Physical Invent: %1", inventOnhand.physicalInvent()));
    info(strfmt("On order: %1", inventOnhand.onOrder()));

    }

    ReplyDelete

Note: Only a member of this blog may post a comment.