As part of the Winter’15 release salesforce introduced the List View API features which helps us to get details and records of list views for an object. I was started exploring more about the List view API and trying to build an use case using that. So I searched about List View in the IdeaExchange site then found that many users are looking for a solution to export records to excel directly from the list view.
I built a solution for that Idea, which helps user to download all the records directly from the List view for both standard and custom object. You don’t have to make any change in the code to use this functionality for different objects. All you need is to create a custom button for that object as mentioned below. Lets take an example for Account object
1. Go to Setup –> Accounts –> Buttons, Links and Actions
2. Click ‘New Button or Link'
3. Enter Label, Name and select display type as ‘List Button’

4. Content source as “Onclick Javascript” and paste the below code
Then include the button in the List view page of the object. Now open any list view of the account object and click the ‘Export to Excel’ button. It will export all records of that list view. Repeat the above steps for creating button for other objects.
Lets see the logic behind the scene, firstly lets take a look into the ListView API. Currently ListView API is available in both SOAP and REST format. Following are the functionalities you can call using REST service. Documentation link
List Views - Get the List of List views for a particular object. Run the following in the workbench to get the result.
Next moving on the code the apex class will call the list view API describe function using HTTP callouts. Then using the JSON parser, parse the JSON string output to get details like SOQL and Columns information. Once we get the SOQL query of the list view then its not a big deal to display the records in a visualforce page. The table in the visualforce page will display the columns dynamically based on the columns in the list view.
1. Go to Setup->Remote Site –> New
2. Add the Name and in the URL enter the domain name of your org
3. Then click save
So now we can able to export records directly from the list view. I have tested this functionality with around 20K records. You can also check my previous article here to exporting more records without hitting view state limit or collection size limits. The complete source code is available here.
You can also deploy to your org by clicking the below button

I built a solution for that Idea, which helps user to download all the records directly from the List view for both standard and custom object. You don’t have to make any change in the code to use this functionality for different objects. All you need is to create a custom button for that object as mentioned below. Lets take an example for Account object
1. Go to Setup –> Accounts –> Buttons, Links and Actions
2. Click ‘New Button or Link'
3. Enter Label, Name and select display type as ‘List Button’

4. Content source as “Onclick Javascript” and paste the below code
//Getting List Name
var listview = document.getElementsByName('fcf')[0];
var listName = listview.options[listview.selectedIndex].text;
//Getting List Id
var listId = document.getElementsByName("fcf")[0].value;
var ObjectName = 'Account';
//Passing ListId,ObjectName and ListName to Visualforce page
window.open("apex/exportStandardListView?Object="+ObjectName+"&listid="+listId+"&listName="+listName,"myWindow");Then include the button in the List view page of the object. Now open any list view of the account object and click the ‘Export to Excel’ button. It will export all records of that list view. Repeat the above steps for creating button for other objects.

ListView API in Winter15:
Lets see the logic behind the scene, firstly lets take a look into the ListView API. Currently ListView API is available in both SOAP and REST format. Following are the functionalities you can call using REST service. Documentation link
List Views - Get the List of List views for a particular object. Run the following in the workbench to get the result.
URL - /services/data/v32.0/sobjects/Account/listviewsDescribe - It will provide the definition of particular list view, including SOQL.
URL - /services/data/v32.0/sobjects/Account/listviews/{ListId}/describeResults – It will return the records of the particular list view including column details.
{ListId} – Enter the ID of the List view
URL - /services/data/v32.0/sobjects/Account/listviews/{ListId}/resultsTo know more about the List view API functionality check the "Super ListView viewer" by Andrew Fawcett
{ListId} – Enter the ID of the List view
Next moving on the code the apex class will call the list view API describe function using HTTP callouts. Then using the JSON parser, parse the JSON string output to get details like SOQL and Columns information. Once we get the SOQL query of the list view then its not a big deal to display the records in a visualforce page. The table in the visualforce page will display the columns dynamically based on the columns in the list view.
//HTTP callouts
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');
String domainUrl=URL.getSalesforceBaseUrl().toExternalForm();
String endpointUrl=domainUrl+'/services/data/v32.0/sobjects/'+ObjectName+'/listviews/'+listid+'/describe';
req.setEndpoint(endpointUrl);
req.setMethod('GET');
Http h = new Http();
HttpResponse res = h.send(req);
Map<String,Object> root = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
//this logic to over come the 10k collection limit
for(Sobject sobj : Database.query((string)root.get('query'))){
recordList.add(sobj);
if(recordList.size() == 10000){
allRecords.add(recordList);
recordList = new List<sobject>();
}
}
if(recordList != null && !recordList.isEmpty())
allRecords.add(recordList);
//Parsing to get the column details
JSONParser parser = JSON.createParser(res.getBody());
while (parser.nextToken() != null){
if(parser.getCurrentToken() == JSONToken.START_ARRAY) {
while (parser.nextToken() != null) {
if(parser.getCurrentToken() == JSONToken.START_OBJECT) {
listviewAPI.Columns le = (listviewAPI.Columns)parser.readValueAs(listviewAPI.Columns.class);
parserCol.add(le);
}
}
}
}The reason why I’m not using the results listview API functionality in my code is, the size is default to 25. So we can’t get more than 25 records from the list view using REST API.
Remote Site Setting:
Since we are making HTTP callouts in apex class, we have to setup remote site to call salesforce by itself. Follow the below steps1. Go to Setup->Remote Site –> New
2. Add the Name and in the URL enter the domain name of your org
3. Then click save
So now we can able to export records directly from the list view. I have tested this functionality with around 20K records. You can also check my previous article here to exporting more records without hitting view state limit or collection size limits. The complete source code is available here.
You can also deploy to your org by clicking the below button

