4

Can we access listview records of any object using listview id?

0

2 Answers 2

6

I'm glad you asked this question because I didn't know this was possible but a quick look through the Schema in the force.com IDE shows that list view is a valid SObject available for querying:

SELECT Id FROM ListView WHERE SObjectType = 'Case'

So it should be possible to treat it like a normal object. Here is a list of the fields I found for it:

Fields:

  • CreatedById
  • CreatedDate
  • DevelperName
  • Id
  • IsSoqlCompatible
  • LastModifiedById
  • LastModifiedByDate
  • LastReferenceDate
  • LastViewedDate
  • Name
  • NamespacePrefix
  • SObjectType
  • SystemModstamp

Child Relationships:

  • ListViewChartInstance
  • NavigationMenuItem
  • UserListView

Other Objects that Might Be Of Insterest:

  • ListViewChart
  • ListViewChartInstance

Even More Information:

Test Controller

public class listViewTestPage {
    private ApexPages.StandardController con;

    public listViewTestPage(ApexPages.StandardController stdCon) {
        con = stdCon;
    }

    public void go() {
        // Invoke the standard controller's list method to get a reference 
        // to the most recently viewed list view for the provided standard
        // controller's object (I tested with case standard controller)
        PageReference lastPage = new ApexPages.action('{!list}').invoke();
        system.debug(string.valueOf(lastPage));

        // Unfortunately this URL doesn't contain the ID of the list view.
        // In testing with case the url is always "/500" which than triggers
        // the server to determine the proper list view.
        system.debug(lastPage.getUrl());

        // Returns an empty map
        system.debug(lastPage.getParameters());
        system.debug(lastPage.getHeaders());

        // More page reference methods for experimenting
        // https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_methods.htm
    }
}

Test Page

<apex:page standardController="Case" extensions="listViewTestPage" action="{!go}">
   <!-- 
       The go() method should generate a debug log to display the requested information. 
       The controller does not actually complete the redirect to simplify testing via dev console
       (every time you save, the page refreshes and the logs will automatically generate).
    -->
</apex:page>

Unfortunately, this page seems to imply there is no way to grab the ID of the most recently viewed listview from the standard controller, even though the controller does have the ability to redirect users back to the list view by using the methods above.

Obtaining the records displayed in a list view by the list view ID:

From what I can tell this is not possible. The ListView object available to us doesn't provide access to the query that it is running to pull in records. I searched the the documentation on SFDC and we simply aren't provided the ability to do that.

I also checked the meta-data API to see if list views was a meta-data type accessible through this API and it doesn't seem so.

You might be able to generate a query from a report using the meta-data API

3
  • I don't think that's at all what the OP was asking.
    – Adrian Larson
    Commented Apr 2, 2017 at 16:11
  • @AdrianLarson I was still in the process of dumping data into my answer when you posted that comment. Please re-review it to see if you think there is enough information now. If my answer is still not addressing OP's issue, then OP needs to include more than a single sentence in the question.
    – gNerb
    Commented Apr 2, 2017 at 16:34
  • @Toby The Original Poster may have been wanting to get the records displayed for a certain list view given the ID of the List View, but I was not, I was looking for exactly this answer and you just MADE MY DAY! Thanks a lot. We may want to consider creating another question addressing this and have you answer it there. AdrianLarson , nice attention to detail. I don't think he was trying to be critical. List views are metadata API accessible as a child tag inside of any object available through CustomObject type. Commented May 10, 2017 at 20:01
2

Yes you can!

First get the sObjectType of the ListView by querying like

SELECT Id, sObjectType FROM ListView WHERE Id = :yourId

Then simply make a callout to your own org with a URL like

/services/data/v40.0/sobjects/[sObjectType]/listviews/[yourId]/describe

You will get back an object with (amongst others) a query attribute. Use that to query your records.

Making a callout to your own org has become even easier since spring 19 because you don't need a Remote Site Setting and can use UserInfo.getSessionId() even in async jobs.

Good luck!

3
  • V should be small in listView Commented Mar 6, 2019 at 9:49
  • @MekyushJariwala where? Does the solution not work without it? Commented Mar 6, 2019 at 13:18
  • No its workring V should be small in URL eg.g /listviews/ Commented Mar 6, 2019 at 17:00

You must log in to answer this question.

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