[X++] Get all sizes for a product by code

When dealing with product dimensions by code, a developer can have a hard time finding the right combinations of dimensions and understanding the hardly normalized table structure behind the products in AX 2012.

I was searching for an elegant way to get all available combinations of the product dimension Size for a specific product.

The most elegant way I found was the following peace of code:

static void GetAvailableSizesForProduct(Args _args)
{
    ItemId              itemId              = "MyItem";
    EcoResProductRecId  ecoResProductRecId  = InventTable::itemProduct(itemId);
    List                listSizes;
    ListEnumerator      le;

    listSizes = EcoResProductVariantDimValue::newProductVariantDim_InventSizeId().getDimValues(ecoResProductRecId);

    le = listSizes.getEnumerator();
    while (le.moveNext())
    {
        info(le.current());
    }
}

This method has the InventTable as starting point. The method static itemProduct on InventTable returns the RecId of the product, which we need to retrieve all available sizes related to the product.
The method EcoResProductVariantDimValue::newProductVariantDim_InventSizeId() initialzes a new instance of class EcoResProductVariantDimValue telling the instance we want to work with the dimension Size and initializing all the relevant parameters and dependencies for us. Then we simply have to call getDimValues to retrieve a list with the available dimensions of type Size.

Robin

Software Engineer / Application Developer

Subscribe to robsCode

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!