web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

How to get Category Hierarchy of a Product using X++

Muhammad Yasir Profile Picture Muhammad Yasir 1,023

Today i am going to show how we can get the Category Hierarchy of a product using X++ code.It's a handy code in various situations where we want to display the complete hierarchy of the a product.

Here is the code snippet,

    ItemId                                itemId = "0036";  // Laptop Backpack
    EcoResProductCategory   EcoResProductCategory;
    EcoResCategory               EcoResCategory,EcoResCategoryNext;
    EcoResCategoryId            parentCategory;
    List                                    li = new List(Types::String);
    ListEnumerator                 enumer;

     select * from EcoResCategory
        join RecId from EcoResProductCategory
            where EcoResCategory.RecId == EcoResProductCategory.Category
            && EcoResProductCategory.Product == InventTable::find(itemId).Product;

    parentCategory = EcoResCategory.ParentCategory;

    while (parentCategory)
    {
        li.addStart(EcoResCategory.Name);

        select * from EcoResCategory
            where EcoResCategory.RecId ==  parentCategory
            && EcoResCategory.ParentCategory != 0;  // skip the top node

        parentCategory = EcoResCategory.ParentCategory;
    }

    enumer = li.getEnumerator();
    while (enumer.moveNext())
    {
        info(enumer.current());
    }

This is what the result looks like.




Happy DAXing :)

This was originally posted here.

Comments

*This post is locked for comments