Category Hierarchy of a Product

refer:- https://community.dynamics.com/ax/b/yasirsmicrosoftdynamicsaxworkshop/archive/2015/12/14/how-to-get-category-hierarchy-of-a-product-usinPg-x

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());
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s