Skip to main content
Dynamics 365 Community / Forums / Finance forum / Adding products to Pro...
Finance forum
Answered

Adding products to Product category hierarchy

editSubscribe (0) ShareShare
ReportReport
Posted on by 82
Hi, I have a requirement where i need to add a validation on product categories  while assigning products to it.
So the flow is something like this where if there is a parent category say Contoso Speakers and it 3 categories as below-
Consoto Speaker
1. Sales product speakers 
2. Assemblies
3. Components
 
Now there requirement is if i add any product to these categories a validation should run, where if i am adding any product lets say ProductA to Sales product speakers >> Speaker set, the same product can be added to any category under Sales product speakers (i.e. it can be added to car speaker & HiFi speakers) but if i try to add the same product to assemblies, assemblies>>cabinetAssemblies etc, or components it should throw a validation saying that the product is part of Sales Product speakers.
Similarly if i have a Product kets say Item12 and it is added to components>>Mechanical then it can be added to any category under components say cabels, packaging, electronics and cabinents but it cannot be added to any other category (assemblies or Sales Product speakers, Sales Product speakers>>car speakers).
Can someone please help me with this?
 
  • Community member Profile Picture
    Community member 82 on at
    Adding products to Product category hierarchy
    Thanks everyone for your time and reply
  • Verified answer
    Layan Jwei Profile Picture
    Layan Jwei 2,746 Super User on at
    Product category hierarchy having validation on products added
    Hi Community User,

    In this case, the approach I suggested in the previous reply should work (you could also maybe approach it in another way). Did you reach anywhere with the link  I provided?

    I wrote something quickly that should work -- please note that your code can be enhanced, but I can't do that, so i just tried to add to your code and amend it a little (due to lack of time) -- what i did is that i just transformed it to  extension instead of event handler and added the recursive function  --I'm not sure if it will 100% work but it should give u an idea now when you debug
    [ExtensionOf(formStr(EcoResCategoryAddProduct))]
    final class EcoResCategoryAddProduct_Extension
    {
        public RefRecId  recId;
       
        public void closeOk()
        {
            FormRun         callerForm = this.args().caller();
            FormDataSource  fdsEcoResProduct;
            EcoResProduct   selectedProduct;
            EcoResCategory  ecoResCategory;
            container       productErrorMessages;
            if (callerForm && this.args().Caller().name() == formStr(EcoResCategory))
            {
                ecoResCategory = callerForm.dataSource(formDataSourceStr(EcoResCategory, EcoResCategory)).cursor() as EcoResCategory;
                fdsEcoResProduct = this.dataSource(formDataSourceStr(EcoResCategoryAddProduct, EcoResProductSelected));
    
                for (selectedProduct = fdsEcoResProduct.getFirst(1)?fdsEcoResProduct.getFirst(1):fdsEcoResProduct.cursor();
                selectedProduct;
                selectedProduct = fdsEcoResProduct.getNext())
                {
                    productErrorMessages += this.checkGroup(selectedProduct, ecoResCategory);
                }
                if(productErrorMessages != conNull())
                {
                    throw error (strfmt("@SYS82282", productErrorMessages));
                }
            }
    
            next closeOk();
        }
    
        /// <summary>
        /// This method checks if the product is already assigned to any category hierarchy
        /// </summary>
        /// <param name = /_ecoResProduct/>Product</param>
        /// <param name = /_ecoResCategory/>Category</param>
        /// <returns>error message for product which has category already assigned</returns>
        public Container checkGroup(EcoResProduct _ecoResProduct, EcoResCategory _ecoResCategory)
        {
            EcoResProduct          ecoResProductSelected = _ecoResProduct;
            EcoResProductCategory  ecoResProductCategory;
            EcoResCategory         ecoResCategorySelected = _ecoResCategory;
            EcoResCategory         ecoResCategoryLocal;
            container               productCatErrorMessage;
            //select firstonly ecoResProductSelected where ecoResProductSelected.RecId == _ecoResProduct.RecId;
            //select firstonly ecoResCategorySelected where ecoResCategorySelected.RecId == _ecoResCategory.RecId;
    
            EcoResCategoryId parentCateogry = this.getParentCategoriesRecursive(ecoResCategorySelected.RecId);
    
            if(parentCateogry)
            {
                select firstonly ecoResProductCategory
                        where ecoResProductCategory.Product == ecoResProductSelected.RecId
                    join ecoResCategoryLocal where ecoResProductCategory.Category == ecoResCategoryLocal.RecId
                        && ecoResCategoryLocal.CategoryHierarchy == ecoResCategorySelected.CategoryHierarchy;
     
                if (ecoResProductCategory)
                {
                    EcoResCategoryId parentCateogryForProduct = this.getParentCategoriesRecursive(ecoResCategoryLocal.RecId);
                    if(parentCateogryForProduct != parentCateogry)
                    {
                        productCatErrorMessage += strFmt("@RET260198",
                                        EcoResProduct::find(ecoResProductSelected.RecId).productName(),
                                        EcoResCategory::find(ecoResProductCategory.Category).Name);
                    }
                }
            }
    
            return productCatErrorMessage;
        }
    
    
        public EcoResCategoryId getParentCategoriesRecursive(EcoResCategoryId _category)
        {
            EcoResCategory ecoResCategory;
            while select ecoResCategory order by ecoResCategory.RecId desc  where ecoResCategory.RecId == _category &&  ecoResCategory.ParentCategory != 0
            {
                recId =  ecoResCategory.RecId;
                this.getParentCategoriesRecursive(ecoResCategory.ParentCategory);
            }
                                                 
            return   recId;
        }
    
    
    }


    Please update us back if it works and verify the answers that helped you.
     
     



    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
  • Community member Profile Picture
    Community member 82 on at
    Product category hierarchy having validation on products added
    Hi Lyan Jwei,
     
    - Please specify all places i can add "product A" to, in the example i put above.
    Lets consider the structure u gave now for parent 1 scenario:
    If productA is added to child 1-1 or child 1-1-1 it can be added to all the places under child1 be it child 1-2 or child 1-2-1 or child 1-3 but it cannot be added to child 2 or 3
    ​​​​​​​

    - Also you know that there could be Parent 2, in this case, can items in parent 1 or items added to children of Parent 1 be repeated in Parent 2?
    Yes if there is a parent 2 in that case the product can be added there because it works indeprndently  but again if it has subCategories then again the above scenario should be taken in consideration e.g. if there s the below hierarchy then if should follow again the validations that a product which is part of child 2.1 can be a part of child 2.1-1, 2.1-1-1 or 2.1-2 but cannot be a part of child 2.2
    Parent 2
        Child 2.1

               Child 2.1-1   (contains product A already)
                     Child 2.11-1-1
               Child 2.1-2
        Child 2.2
               Child 2.2-1
        Child 2.3
     
    Please let me know the x++ code for the same or the changes i can do in above code?
     
  • Layan Jwei Profile Picture
    Layan Jwei 2,746 Super User on at
    Product category hierarchy having validation on products added
    Hi Community User,

    You didn't first confirm if what I understood is correct or not.

    So let's say you have this tree:

    Parent 1(Contoso Speakers)
        Child 1
               Child 1-1   (contains product A already)
                     Child 1-1-1
               Child 1-2
        Child 2
               Child 2-1
        Child 3
     
     
    As you see product A already exists in Child 1-1 only... now what other place can i add it to?
    can i only add product A to Child 1-1-1? OR can i also add it to Child 1 and Child 1-2 and Parent 1?

    - Please specify all places i can add "product A" to, in the example i put above.
    - Also you know that there could be Parent 2, in this case, can items in parent 1 or items added to children of Parent 1 be repeated in Parent 2?


    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
  • Community member Profile Picture
    Community member 82 on at
    Product category hierarchy having validation on products added
    Hie, can you please tell me what modifications can i do in the above case
  • Suggested answer
    Layan Jwei Profile Picture
    Layan Jwei 2,746 Super User on at
    Product category hierarchy having validation on products added
    Hi Community User,
     
    I think you will need to use recursive functions. -- you can do some research about that
    Here's a link about recursive functions but you need to write your own to meet your business need
     
    If I understood your requirement correctly, then let's say you are trying to add productA under speakerSets. You need to write code to find the main parent for speakerSets which is sales product speakers
     
    Then you need to do a select statement to see if this productA exisits anywhere in the table. If let's say you found that it exists in electronics. You need to write now code to the find the main parent for electronics which is components... Now you are going to say: does sales product speaker equal components if yes then allow insert if not then don't 
     
    So you will need to use recId, parentCaregory and categoryhirearchy fields
     
    Thanks,
    Layan Jweihan 
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
     
  • Community member Profile Picture
    Community member 82 on at
    Product category hierarchy having validation on products added
    As of now when i click on EcoResCategory>>products>> Add, it takes me to “EcoResCategoryAddProduct” form, and when i seletect the same product and click on OK to add it to any category it throws error saying it’s already part of some category (i.e. ProductA is part of speaker set), but my requirement is is if ProductA is part of speaker set which falls under sales product speaker (parent category)  then if i am adding the product to car speakers or HiFi speakers it shouldn’t throw any error and this validation should only run when productA is being added to other parent categories or othetparentcategory>> categories.
    please refer to above image and explanation for more information, and please help me with the code
  • Andre Arnaud de Calavon Profile Picture
    Andre Arnaud de Cal... 282,978 Super User on at
    Product category hierarchy having validation on products added
    Hi,
     
    Can you tell what exact error you get and when? Is it while using the form or already when you want to build your coding? Besides, I don't think the closeOK() method is the best place. The data is already stored then. The logic wil try to prevent a user from closing the form, but a simple closing of the browser or reload of the page will not block the user to navigate away from the current page.

Helpful resources

Quick Links

What Motivates a Super User?

We know many of you visit the Dynamics 365 Community and Power Platform…

Demystifying Copilot with Sundar…

Industry experts answer burning questions directly from our amazing Community…

Enabling Copilot Case and Conversation…

Agents can easily recap an ongoing chat, transcribe a voice conversation…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 282,978 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,562 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Product updates

Dynamics 365 release plans