Needs to get the category hierarchy from top to bottom Which mean we have c1 code from c1 needs to get c1-->c2-->c3-->c4--> etc,. I have an 'category' object it stores all the category names and codes and having another object 'parent category' which store the hierarchy(relation) of the each category records

Please check the above image for your references Eg: In category object :

Name code id(18digit)
A 10 111XXXX
A1 11 112XXXX
A2 12 113XXXX
A3 13 114XXXX
A11 111 115XXXX
A12 112 116XXXX
B 21 117XXXX
B1 22 118XXXX

Eg : In parent category object Storing the relationship of category Fields :

Label Datatype
ParentCatId Lookup(Category__c)
childCatId Lookup(Category__c)


ParentCatId childCatId
Null A
A A1
A1 A2
A2 A3
A A11
A11 A12
Null B
B B1

The above is the object architecture and sample data for your understanding.

Now currently i am having the record A code(first level code) 10.

  1. 10 -> 11 --> 12-->13
  2. 10 --> 111 --> 112

How can i find using 1) and 2) with the top level category code using apex (without breaking the governor limits in salesforce). Any one can help me on this to achieve using apex.

Please check the below code

public CategoryHelperClass() //Constructor
    //pass first category code 

public static void getCategoryProducts(List<String> catCode){ //Method
    catProdWrapper catProd=new catProdWrapper(); //Wrapper Initialization
    Map<String, List<String>> getcatProd=new Map<String, List<String>>();
    //List<Parent_Category__c> categoryList=new List<Parent_Category__c>();
    List<String> childCatCodes=new List<String>();
    List<String> lastLevelCat=new List<String>();
    List<String> removeList=new List<String>();
    Boolean flag=true;
    //Query to check/get the child category
        for(Parent_Category__c parentCar : [Select Child_Category_ID__r.Id, Child_Category_ID__r.Name, Child_Category_ID__r.Category_Id__c,
                                            Parent_Category_ID__r.Category_Id__c from Parent_Category__c 
                                            Where Parent_Category_ID__r.Category_Id__c IN:catCode]){
                                                if(parentCar != null){
    if(childCatCodes.size() > 0){

I needs to store all the child category in a variable . When i get the level2 category again i cant able to store the level3 child values in the same variable because the method invoking again. how to achieve this can some one help me on this ? Check the below obj structure for your reference in the image. i needs to get the all the last level categories for one parent category.

  • 1
    Welcome to SFSE. We cannot implement your requirements or provide complete designs, but we're happy to help you with issues you encounter in your own implementation process. For more about our format, please take the tour and read How to Ask. Please edit your question to show us what you've tried and where you are stuck. Oct 1 '21 at 18:50
  • 1
    @DavidCheng, thanks I have add the code where i was stucked
    – Vinod
    Oct 3 '21 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.