Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

How to get Category Name of an ItemId of Level 3

Posted on by 190

Hi there, we are developing Stock Analysis report for which there is a field Category Name which needs to populate EcoresCategory.Name value for an ItemId but the Level Should be 3. For exmaple  Take ItemId: 12345 for which the  Hierarchy Levels are  (Level1 > Level2 > Level3 > Level4>Level5).  We need to consider only Level 3 CategoryName and needs to print value accordingly for selected ItemId from InventTrans. Please suggest accordingly. 

CategoryName = EcoresCategory.Name

Level = EcoresCategory.Level  (We need to capture Level - 3)

ItemId = InventTrans.ItemId

Thanks,

Prem

  • Suggested answer
    Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,089 on at
    RE: How to get Category Name of an ItemId of Level 3

    Hi premK6969

    FindCategoryByProduct already returns EcoResCategory and related line of code can be simplified

    InventTable inventTable = InventTable::find(inventTrans.ItemId);
    
    EcoResCategory ecoResCategory = EcoResProductCategory::findCategoryByProduct(inventTable.Product);
    
    while (ecoResCategory.Level > 3) //Create const or parameter in report for this value
    {
        ecoResCategory = ecoResCategory.getParent();
    }
    
    info(ecoResCategory.Name); //Name of 3rd level category

  • premK6969 Profile Picture
    premK6969 190 on at
    RE: How to get Category Name of an ItemId of Level 3

    Hi Sergei,  i tried with the code you provided but cant able to see the expected result. Actually i need to place this code in a method and call it in report RDP class to get the catgegory name as per the ItemId. Please find the code attached 

              ItemId _itemid;
            _itemid = any2str(852012);
            InventTable inventTable = InventTable::find(_itemid);  // For testing i tried it with ItemId = 852012
    
            EcoResCategory ecoResCategory = EcoResCategory::find(EcoResProductCategory::findCategoryByProduct(inventTable.Product).CategoryHierarchy); // Here you mentioned Category which is throwing error so i changed to Categoryhierarchy
    
            while (any2Int(ecoResCategory) > 3) //Create const or parameter in report for this value
            {
                ecoResCategory = ecoResCategory.getParent();   // While debugging here it is throwing exception error
            }
    
            info(ecoResCategory.Name); //Name of 3rd level category

  • Suggested answer
    Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,089 on at
    RE: How to get Category Name of an ItemId of Level 3

    Hi premK6969,

    You should get something like that in the end (without the caching part)

    InventTable inventTable = InventTable::find(inventTrans.ItemId);
    
    EcoResCategory ecoResCategory = EcoResCategory::find(EcoResProductCategory::findCategoryByProduct(inventTable.Product).Category);
    
    while (ecoResCategory.Level > 3) //Create const or parameter in report for this value
    {
        ecoResCategory = ecoResCategory.getParent();
    }
    
    info(ecoResCategory.Name); //Name of 3rd level category

  • premK6969 Profile Picture
    premK6969 190 on at
    RE: How to get Category Name of an ItemId of Level 3

    Hi Sergei, Can u please be more specific since am new to this .

  • Suggested answer
    Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,089 on at
    RE: How to get Category Name of an ItemId of Level 3

    Hi premK6969,

    You can find the category for the product with EcoResProductCategory: InventTable (use ItemId to find the record) -> EcoResProductCategory.Product = InventTable.Product ->EcoResCategory.RecId = EcoResProductCategory.Category

    Then you need to check if the level is greater than 3, then find the parent category via the parent category field (EcoResCategory.RecId  = EcoResCategory.ParentCategory) until the level is not equal to 3.

    Also, you can cache the search result and store in map pairs "Item category" - "3rd level category" to speed the search process and first lookup 3rd level from the map and if it's not found in the map, search from tables and put the result to map.

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans