function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
guenthmnguenthmn 

Query for list view Id's and names

How do I query for all the list view names and ids for a specific sObject via apex?

 

I don't see this in the API or as a describe object option.

 

Thanks,

 

Matt

Best Answer chosen by Admin (Salesforce Developers) 
zachbarkleyzachbarkley

Hi Lakshmi,

 

As far as I know, you cannot access list views via SOQL queries. It's built into the SObject. We created a method which might help you see how we use List Views in APEX. Our Task was to get a LIST VIEW URL based on the LABEL (view name). You cannot retrieve ID's based on the LIst View API Name as the describe sobject only returns a ListOption which is a value and label.

 

Requirement


Display a link on your own visualforce page. When clicked it will take a user to the the All Accounts List View for the Account Object.

 

    /********************************
    * VISUALFORCE
    ********************************/
    <apex:page controller="myListViewURLController" >
	<apex:form>
        	<apex:outputLink value="{!AllAccountsListViewURL}">All Accounts</apex:outputLink> 
        </apex:form>
    </apex:page>
    
    /*******************************
    * APEX
    *******************************/
    
    public class myListViewURLController {
    	Public AllAccountsListViewURL {get;set;}
    	public myListViewURLController (){
            /******************************
            * GET OBJECT PREFIX LIST
            ******************************/
            Map<String,String> PrefixList = PrefixList();
    
    	    /*****************************
            * SET OBJECT AND LIST VIEW LABEL NAME
            *****************************/
            AllAccountsListViewURL = 
            	ListViewURL(PrefixList,'Account','All Accounts');
        }
        Public Map<String,String> PrefixList(){
            Map<String,String> PrefixList = new Map<String,String>{};
            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
            for(String sObj : gd.keySet()){
                Schema.DescribeSObjectResult r =  gd.get(sObj).getDescribe();
                PrefixList.put(r.getName(), r.getKeyPrefix());
            }
            return PrefixList;
        } 
        Public String ListViewURL(Map<String,String> PrefixList,String ObjectName,String ListViewLabel){
            String ListViewURL;
            String ListViewId;
            String q = 'SELECT Name FROM '+ ObjectName +' LIMIT 1';
            ApexPages.StandardSetController ACC = new ApexPages.StandardSetController(Database.getQueryLocator(q));
            List<SelectOption> ListViews = ACC.getListViewOptions();
            for(SelectOption w : ListViews ){
                if(w.getLabel()==ListViewLabel){
                    ListViewId = w.getValue().left(15);
                    ListViewURL='/'+PrefixList.get(ObjectName)+'?fcf='+ListViewId;
                }
            }
            return ListViewURL;
        }
    }

 

Zach

All Answers

henryCHhenryCH

Good question, this would be very useful for the enhancedList VF component.

 

Any ideas??

 

Henry

ca_peterson_oldca_peterson_old

Here's a post on how to get it working, along with all the needed workaround for a bug you're going to hit if you use a dynamic listId.

https://lameness.info/wordpress/11.05.2010/the-apexenhancedlist-component-is-in-fact-broken/

 

It's an ugly hack on top of an ugly hack, but it works well so far. Just don't use them inside an apex:panelBar or they don't work right.

MARATHONMARATHON

That link above is borked.  Was it a typo?

 

I've also had trouble obtaining these IDs.  Eclipse ID only pulls the list view 'name' and 'label'.

nagalakshminagalakshmi

Hi Peterson,

 

The link which you have provide is not opened now. Can you help me how to use the listviewid in salesforce queries. Please help me..

 

Thanks,

Lakshmi.

zachbarkleyzachbarkley

Hi Lakshmi,

 

As far as I know, you cannot access list views via SOQL queries. It's built into the SObject. We created a method which might help you see how we use List Views in APEX. Our Task was to get a LIST VIEW URL based on the LABEL (view name). You cannot retrieve ID's based on the LIst View API Name as the describe sobject only returns a ListOption which is a value and label.

 

Requirement


Display a link on your own visualforce page. When clicked it will take a user to the the All Accounts List View for the Account Object.

 

    /********************************
    * VISUALFORCE
    ********************************/
    <apex:page controller="myListViewURLController" >
	<apex:form>
        	<apex:outputLink value="{!AllAccountsListViewURL}">All Accounts</apex:outputLink> 
        </apex:form>
    </apex:page>
    
    /*******************************
    * APEX
    *******************************/
    
    public class myListViewURLController {
    	Public AllAccountsListViewURL {get;set;}
    	public myListViewURLController (){
            /******************************
            * GET OBJECT PREFIX LIST
            ******************************/
            Map<String,String> PrefixList = PrefixList();
    
    	    /*****************************
            * SET OBJECT AND LIST VIEW LABEL NAME
            *****************************/
            AllAccountsListViewURL = 
            	ListViewURL(PrefixList,'Account','All Accounts');
        }
        Public Map<String,String> PrefixList(){
            Map<String,String> PrefixList = new Map<String,String>{};
            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
            for(String sObj : gd.keySet()){
                Schema.DescribeSObjectResult r =  gd.get(sObj).getDescribe();
                PrefixList.put(r.getName(), r.getKeyPrefix());
            }
            return PrefixList;
        } 
        Public String ListViewURL(Map<String,String> PrefixList,String ObjectName,String ListViewLabel){
            String ListViewURL;
            String ListViewId;
            String q = 'SELECT Name FROM '+ ObjectName +' LIMIT 1';
            ApexPages.StandardSetController ACC = new ApexPages.StandardSetController(Database.getQueryLocator(q));
            List<SelectOption> ListViews = ACC.getListViewOptions();
            for(SelectOption w : ListViews ){
                if(w.getLabel()==ListViewLabel){
                    ListViewId = w.getValue().left(15);
                    ListViewURL='/'+PrefixList.get(ObjectName)+'?fcf='+ListViewId;
                }
            }
            return ListViewURL;
        }
    }

 

Zach

This was selected as the best answer
tggagnetggagne

So, if I'm dynamically building a <apex:enhancedList listId="{!listViewId}"  /> what everyone's saying is there's no way to do it in Apex because the list view IDs aren't available--not even using the (broken) StandardSetController.GetListViewOoptions()?

So the only alternative is to make metadata calls from Apex back to my org to find the list views?