How to Query the ListView of an Object in Salesforce SOQL?

In Salesforce, we can query information related to the List View using the ListView object, which provides queries for the public list view of the sObjects.

By using the ListView object in SOQL queries, we can only query public list views or list views accessed by the user executing the query on the sObjects, meaning private list views cannot be queried in SOQL.

In this Salesforce tutorial, I will explain how to query the ListView of an object in Salesforce SOQL.

Query the ListView of an Object in Salesforce SOQL

To query the list view of a Salesforce object in SOQL, we will use the standard object ListView.

For example, we have to query the list views of the Account object, and then we can execute the SOQL query below.

SELECT Id, Name, DeveloperName, SobjectType 
FROM ListView 
WHERE SobjectType = 'Account'

Output:

SOQL List view of objects in Salesforce

This way, we can execute the SOQL query to fetch the list view of a specific object in Salesforce.

Let’s take another example, where we will query the username of the user who created those list views for that specific object.

In the SOQL query below, we will query the list view of the Account object, along with the names of users who created those list views.

SELECT Name, CreatedBy.Name 
FROM ListView
WHERE SobjectType = 'Account'

Output:

Salesforce SOQL to query List view with user name

This way, we can query the Salesforce List views in SOQL queries using the ListView objects.

Export List Views with Shared to Details Using SOQL in Salesforce

In this example, we will export the object records from a specific list view into an Excel or CSV file, similar to exporting opportunity records that appear when you set the list view to ‘Closing this month’.

We will execute the SOQL query in the Salesforce Workbench to retrieve the desired record with the filter conditions specified in the list view. Now, open the Salesforce Workbench and follow the steps below.

  1. First, we need the ID of the list view for which you want to export records. To get the list view ID, navigate to the list view and copy the 15-digit code or list view API name from the URL.
    • In my case, it showed the API name of the list view.
SOQL List view records in Salesforce
  1. After this, log in to your Salesforce Workbench account and ensure that you select “Production” in the Environment option.
    • In the Utilities tab, select the REST Explorer option.
Export Salesforce Object list view using bulk API
  1. In the REST Explorer, select the Get option and execute the URL below.
/services/data/v62.0/sobjects/Opportunity/listviews/ClosingThisMonth/describe

Replace the Opportunity with the object API name and ClosingThisMonth with the list view API name or the ID we got from the list view URL.

Download the Object List view in Salesforce
  1. As we execute the URL, it will generate the SOQL query in the response body Order by section.
    • Copy the generated SOQL query to the clipboard.
Salesforce SOQL query and export object List view records
  1. Navigate to the queries tab, select the SOQL query, and paste the copied query into the query editor. For the test run, select ‘View as List’.
    • When the query generates the desired records, select ‘View as Bulk CSV‘ and then execute the query again.
    • When the bulk API job is finished, it returns the batch result. From there, we must click on the document icon to download the records stored in a CSV file.
Salesforce SOQL export Object List view records

This way, we can download the list view records into a CSV file using the SOQL in the Salesforce workbench.

Errors in executing the SOQL query to get the list view:

The most common error you can face while executing the SOQL query is getting the parent relationship query error.

Error in SOQL query to get List view record in Salesforce

If the records you are querying have a parent object, then you must change the workbench settings.

In the settings, navigate to the Query and Search options section and select the checkbox That Allows SOQL Parent Relationship Queries.

Allow Parent relationship queries in Salesforce SOQL

By applying this setting, we can resolve the parent relationship query error in the Salesforce workbench.

Conclusion

In Salesforce, we can efficiently query the object’s list view by following the above methods, where we have learned to query the information related to the object list view for different scenarios.

Additionally, we have utilized the Salesforce Workbench to query and export the list view records of a specific Salesforce object. Additionally, I have explained the solution to resolve the parent relationship query error that occurs when executing the SOQL query in the Workbench.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.