0

I'm trying to solve this error, but everything that I have been trying hasn't resolved it. No problems appear when I save the file, but when I preview the visual force page, the "Too many SOQL queries" error appears. I've separated the line that it links the error to. If anyone has some insight as to why this error keeps popping up, it would be greatly appreciated, as I don't have a lot of knowledge on SOQL queries currently.

We are trying to create a drop-down on a Visualforce page that pulls through all Contact List Views in Alphabetical order. Currently, they are showing up, but they are sorted by ID, causing users to become frustrated when they cannot find their assigned lists.

public class ContactListViewClass {
   public List<SelectOption> listViewNames{get; set;}
   public String conFilterName{get; set;}

   public ContactListViewClass(ApexPages.StandardController controller) {
ApexPages.StandardSetController mySetCon = new ApexPages.StandardSetController([SELECT Id FROM Contact LIMIT 1]);

   List<Id> listViewIds = new List<ID>();

   for(SelectOption so :mySetCon.getListViewOptions()){
       listViewIds.add(so.getValue());
       }

   for(ListView lv :[SELECT Id, Name FROM ListView WHERE ID IN :listViewIds ORDER BY Name ASC]){
       List<SelectOption> listViewNames = new List<SelectOption>();
       listViewNames.add(new SelectOption(lv.Id, lv.Name));

        String q = 'SELECT Name FROM Contact LIMIT 1'; //Any object whose list view you are trying to retrieve. 
    ApexPages.StandardSetController ACC = new ApexPages.StandardSetController(Database.getQueryLocator(q));
        List<SelectOption> ListViews = ACC.getListViewOptions();
      //  List<SelectOption> listViewNames = new List<SelectOption>();
        for(SelectOption w : ListViews ){
            listViewNames.add(new SelectOption(w.getLabel(), w.getLabel()));
            }
      //listViewNames= [SELECT Name,SobjectType FROM ListView WHERE SobjectType = 'Contact' order by Name DESC];
        } 
    }
}

visualforce page

    <apex:selectList value="{!conFilterName}" size="1" id="filterMenu">
        <apex:selectOptions value="{!listViewNames}" />
        <!-- <apex:actionSupport event="onchange" action="{!resetFilter}" rerender="contactENlistview" status="ajaxStatus"/>-->
    </apex:selectList> 

Updated Code as of 4:22 pm pacific

      public class NitishContactListViewClass {

       public List<SelectOption> listViewNames{get; set;}
       public String conFilterName{get; set;}

       public NitishContactListViewClass(ApexPages.StandardController controller) {
       List<SelectOption> listViewNames = new List<SelectOption>();

       for(ListView lv :[SELECT Id, Name FROM ListView WHERE SObjectType='Contact' ORDER BY Name ASC]){
            listViewNames.add(new SelectOption(lv.Id, lv.Name));

        }
    }
    }

Current visual of visualforce page

enter image description here

0

When you are creating a SetController (the line you separated) you're, in essence, running a SOQL query. You can't do that inside of a for-loop or you'll hit a governor limit (as you just have).

Also, you are creating and destroying the listview collection inside the loop every time.

The simplified code below will give you all the listviews for the Contact object.

public ContactListViewClass(ApexPages.StandardController controller) {

   listViewNames = new List<SelectOption>();

   for(ListView lv :[SELECT Id, Name FROM ListView WHERE SObjectType='Contact' ORDER BY Name ASC]){
       listViewNames.add(new SelectOption(lv.Id, lv.Name));

    }
}

NOTE: If you add more details (in your original post) to what you're trying to do, I can try to refine this code

11
  • Hi Sebastian, I have updated the question! We are trying to organize a drop-down list of List Views for users on a Visualforce page. Currently, they show up but are sorted by List View ID, causing users to be unable to find the views they need. Apr 16 '20 at 22:51
  • Did you try the snippet I put there? Should do just that, assuming you want "Contact" listviews Apr 16 '20 at 22:52
  • Yes, I'm now receiving error: SELECT Id, Name FROM ListView WHERE SObjectName='Contact' ORDER BY Name ^ ERROR at Row:1:Column:37 No such column 'SObjectName' on entity 'ListView'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. Apr 16 '20 at 22:59
  • Gosh, that was a typo. It should be SObjectType. I changed the snippet Apr 16 '20 at 23:03
  • Ok, great! I've tried this update, and now I'm seeing my drop-down but it hasn't retrieved any of the list views, so it's empty. (I've got roughly 300 created as a test and have double checked that they are all SObjectType Contact...) thank you so much for helping with this! My coworker and I have been struggling with this code for the vast majority of the day and your help is really appreciated. Apr 16 '20 at 23:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.