0
@RestResource(urlMapping='/CollectionMaster/*')
global with sharing class CollectionMaster{

    @HttpPOST
    global static CollectionWrapper getCollectionMaster(String userName){ 
         list<Product__c> crList;
         list<Product__c> crList1;
         User ur=[select id,Region__c,UserRole.name from  user where Username=:userName];
            if(ur.UserRole.name =='SR')
       {

          crList= [select id,Brand__c,Category__c,Collection__c,Collection_Name__c from Product__c];
          crList1= [select id,Brand__c,Category__c,Collection__c,Collection_Name__c from Product__c];
       }
      else if(ur.UserRole.name =='RSM'){
      crList= [select id,Brand__c,Category__c,Collection__c,Collection_Name__c  from Product__c LIMIt 50000];
       system.debug('***RSM***');
        } else if(ur.UserRole.name =='HOD' || ur.UserRole.name =='MD'){
           crList= [select id,Brand__c,Category__c,Collection__c,Collection_Name__c from Product__c];
           }
list<Brand_Ranking__c>brList=[select id,Brand__c,Ranking__c from Brand_Ranking__c order By Ranking__c desc]; 

        set<String> categoryList1= new set<String>();
        set<String> categoryArrayList= new set<String>();
        set<String> collectionList1= new set<String>();
        set<String> collectionArrayList= new set<String>();
        CollectionWrapper cc=new CollectionWrapper();
          cc.categoryList =new list<String>();
            cc.collectionList=new list<String>();
         system.debug('Category list*******'+crList);
       if(ur.UserRole.name =='SR')
       {
         for(Product__c pd:crList){
         String[] colArray = pd.Collection_Name__c.split('\\;');
          for(String col:colArray )
             {
              collectionList1.add(pd.Collection_Name__c);
              collectionList1.add(col);
             }
         } 

         for(Product__c pd1:crList1){
          String[] colArray2 = pd1.Product__c.split('\\;');
              for(String col:colArray2 )
            {
                categoryArrayList.add(col);
            }
         }



             for(Brand_Ranking__c pdd:brList)
         {
            for(string st:categoryArrayList)
            {
              if(st==pdd.Category__c)
             categoryList1.add(st);
            }
         }


           }else
       {
         for(Product__c pd:crList){
          String[] warArray4 = pd.Collection_Name__c.split('\\;');
         for(String col:warArray4 )
          {
              collectionList1.add(col);
            }

        String[] warArray1 = pd.Product__c.split('\\;');
          for(String col:warArray1 )
            {
              categoryArrayList.add(col);
            } 

         for(Brand_Ranking__c pdd:brList)
          {
            for(string st:categoryArrayList)
          {
              if(st==pdd.Product__c)
        categoryList1.add(st);
            }
         }


        }
        }



       system.debug('categoryList1/********'+categoryList1);
        system.debug('collectionList1/********'+collectionList1);
         categoryList1.remove(null);
           collectionList1.remove(null);
            cc.categoryList.addAll(categoryList1);
              cc.collectionList.addAll(collectionList1);
         return cc;
      }


        global class CollectionWrapper{
       public list<String>categoryList;
        public list<String>collectionList;

       }
    }
6
  • I want the list of category and collection from products to be displayed as output but i am getting this error that limitexception 50001. Commented Jul 23, 2018 at 7:31
  • looks like you have more than 50K Product__c records. put any filter if possible or put limit of 50K
    – Ratan Paul
    Commented Jul 23, 2018 at 7:34
  • Limit 50000 is not working Commented Jul 23, 2018 at 7:34
  • I can't see limit in line no 16 product__c query limit definitely will work
    – Ratan Paul
    Commented Jul 23, 2018 at 7:39
  • crList= [select id,Brand__c,Category__c,Collection__c,Collection_Name__c from Product__c LIMIt 50000]; I tried this this but still the error is same Commented Jul 23, 2018 at 7:40

1 Answer 1

1

The 50,000 row limit is per transaction. That means if you query 20,000 records, and then 30,000 records, and then 1 record, you will break the limit and throw an exception. You are going to need to optimize your code such that it can run with 50,000 rows worth of query data or less. This means not querying rows pointlessly, like this:

      crList= [select id,Brand__c,Category__c,Collection__c,Collection_Name__c from Product__c];
      // This is literally the same query again?
      crList1= [select id,Brand__c,Category__c,Collection__c,Collection_Name__c from Product__c];

And it also means not querying without filters. You should be able to identify some sort of criteria to minimize the amount of rows you need to return. Even if you were able to handle an unlimited number of rows, you'd eventually reach other limits, like CPU time or heap size, so you may as well take the time to fix your code properly.

I'm not going to rewrite your code for you, as it is quite large, but hopefully this information will get you started.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .