├── images └── ExportToExcel.png ├── src ├── classes │ ├── listviewAPI.cls-meta.xml │ ├── listviewApiTest.cls-meta.xml │ ├── mockHttpResponseGenerator.cls-meta.xml │ ├── mockHttpResponseGenerator.cls │ ├── listviewApiTest.cls │ └── listviewAPI.cls ├── pages │ ├── exportStandardListView.page-meta.xml │ └── exportStandardListView.page ├── remoteSiteSettings │ └── ListViewAPI.remoteSite ├── package.xml └── objects │ └── Account.object ├── .gitattributes ├── .gitignore └── README.md /images/ExportToExcel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/karanrajs/ExportfromListView/HEAD/images/ExportToExcel.png -------------------------------------------------------------------------------- /src/classes/listviewAPI.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 32.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /src/classes/listviewApiTest.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 32.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /src/classes/mockHttpResponseGenerator.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 33.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /src/pages/exportStandardListView.page-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 32.0 4 | false 5 | false 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/remoteSiteSettings/ListViewAPI.remoteSite: -------------------------------------------------------------------------------- 1 | 2 | 3 | Update the Remote Site URL as per your org. Change ap2 into your org instance 4 | true 5 | true 6 | https://c.ap2.visual.force.com 7 | 8 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /src/classes/mockHttpResponseGenerator.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | global class mockHttpResponseGenerator implements HttpCalloutMock { 3 | // Implement this interface method 4 | global HTTPResponse respond(HTTPRequest req) { 5 | System.assertEquals('GET', req.getMethod()); 6 | // Create a fake response 7 | HttpResponse res = new HttpResponse(); 8 | res.setHeader('Content-Type', 'application/json'); 9 | res.setBody('{"query":"Select Id from Account","columns":[ {"ascendingLabel" : "Z-A","label" : "Account Name","fieldNameOrPath" : "Account Name","hidden":false}]}'); 10 | res.setStatusCode(200); 11 | return res; 12 | } 13 | } -------------------------------------------------------------------------------- /src/package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | listviewAPI 5 | listviewApiTest 6 | mockHttpResponseGenerator 7 | ApexClass 8 | 9 | 10 | exportStandardListView 11 | ApexPage 12 | 13 | 14 | ListViewAPI 15 | RemoteSiteSetting 16 | 17 | 18 | Account.Export_to_Excel 19 | WebLink 20 | 21 | 31.0 22 | 23 | -------------------------------------------------------------------------------- /src/pages/exportStandardListView.page: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
{!colName}
18 | 19 |
-------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .project 2 | .settings 3 | salesforce.schema 4 | Referenced Packages 5 | 6 | # ========================= 7 | # Operating System Files 8 | # ========================= 9 | 10 | # OSX 11 | # ========================= 12 | 13 | .DS_Store 14 | .AppleDouble 15 | .LSOverride 16 | 17 | # Icon must end with two \r 18 | Icon 19 | 20 | # Thumbnails 21 | ._* 22 | 23 | # Files that might appear on external disk 24 | .Spotlight-V100 25 | .Trashes 26 | 27 | # Directories potentially created on remote AFP share 28 | .AppleDB 29 | .AppleDesktop 30 | Network Trash Folder 31 | Temporary Items 32 | .apdisk 33 | 34 | # Windows 35 | # ========================= 36 | 37 | # Windows image file caches 38 | Thumbs.db 39 | ehthumbs.db 40 | 41 | # Folder config file 42 | Desktop.ini 43 | 44 | # Recycle Bin used on file shares 45 | $RECYCLE.BIN/ 46 | 47 | # Windows Installer files 48 | *.cab 49 | *.msi 50 | *.msm 51 | *.msp 52 | -------------------------------------------------------------------------------- /src/objects/Account.object: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Export_to_Excel 5 | online 6 | massActionButton 7 | javascript 8 | Export to Excel 9 | onClickJavaScript 10 | false 11 | true 12 | //Getting List Name 13 | var listview = document.querySelectorAll('[name$="fcf"]')[0]; 14 | var listName = listview.options[listview.selectedIndex].text; 15 | //Getting List Id 16 | var listId = document.getElementsByName("fcf")[0].value; 17 | var ObjectName = 'Account'; 18 | //Passing ListId,ObjectName and ListName to Visualforce page 19 | window.open("apex/exportStandardListView?Object="+ObjectName+"&listid="+listId+"&listName="+listName,"myWindow"); 20 | 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Export From ListView 2 | ==================== 3 | 4 | 5 | Deploy to Salesforce 7 | 8 | 9 | This simple project allows you to Export records into excel sheet directly from Standard List view of any objects.Find more details in my blog check here 10 | 11 |

Screenshot

12 | ![img](/images/ExportToExcel.png) 13 | 14 | 15 | 16 |

Export to Excel button

17 | To add the Export to excel button follow the below setps, below example for 'Account' object 18 |
    19 |
  1. Go to Setup –> Accounts –> Buttons, Links and Actions
  2. 20 |
  3. Click ‘New Button or Link'
  4. 21 |
  5. Enter Label, Name and select display type as ‘List Button’
  6. 22 |
  7. Content source as “Onclick Javascript” and paste the below code
  8. 23 |
24 | 25 | ``` 26 | //Getting List Name 27 | var listview = document.getElementsByName('fcf')[0]; 28 | var listName = listview.options[listview.selectedIndex].text; 29 | //Getting List Id 30 | var listId = document.getElementsByName("fcf")[0].value; 31 | var ObjectName = 'Account'; //Action : Enter the Object API Name 32 | //Passing ListId,ObjectName and ListName to Visualforce page 33 | window.open("apex/exportStandardListView?Object="+ObjectName+"&listid="+listId+"&listName="+listName,"myWindow"); 34 | ``` 35 | 36 |

Remote Site settings

37 |
    38 |
  1. Go to Setup->Remote Site –> New
  2. 39 |
  3. Add the Name and in the URL enter the domain name of your org
  4. 40 |
  5. Then click save
  6. 41 |
42 | -------------------------------------------------------------------------------- /src/classes/listviewApiTest.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class listviewApiTest { 3 | 4 | @isTest static void testAllAccountList() { 5 | 6 | List testAccounts = new List(); 7 | for(integer i=0; i<100 ; i++){ 8 | Account acc = new Account(); 9 | acc.Name = 'Test Account'+ i; 10 | testAccounts.add(acc); 11 | } 12 | insert testAccounts; 13 | Test.StartTest(); 14 | Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator()); 15 | PageReference pageRef = Page.exportStandardListView; 16 | Test.setCurrentPage(pageRef); 17 | listviewAPI listviewAPIctrl = new listviewAPI(); 18 | pageRef.getParameters().put('listid', 'ert322144'); 19 | pageRef.getParameters().put('Object', 'Account'); 20 | pageRef.getParameters().put('listName', 'All Account'); 21 | listviewAPIctrl.fetchListviewRecords(); 22 | Test.StopTest(); 23 | system.assertEquals(listviewAPIctrl.fileName,'Account_All_Account_'+Datetime.now().format()); 24 | system.assertEquals(listviewAPIctrl.columnName[0],'Account Name'); 25 | 26 | } 27 | 28 | @isTest static void testAllAccountList_error() { 29 | 30 | List testAccounts = new List(); 31 | for(integer i=0; i<100 ; i++){ 32 | Account acc = new Account(); 33 | acc.Name = 'Test Account'+ i; 34 | testAccounts.add(acc); 35 | } 36 | insert testAccounts; 37 | Test.StartTest(); 38 | Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator()); 39 | PageReference pageRef = Page.exportStandardListView; 40 | Test.setCurrentPage(pageRef); 41 | listviewAPI listviewAPIctrl = new listviewAPI(); 42 | pageRef.getParameters().put('listid', ''); 43 | pageRef.getParameters().put('Object', ''); 44 | listviewAPIctrl.fetchListviewRecords(); 45 | Test.StopTest(); 46 | system.assertNotEquals(listviewAPIctrl.fileName,'Account_All_Account_'+Datetime.now().format()); 47 | 48 | } 49 | } -------------------------------------------------------------------------------- /src/classes/listviewAPI.cls: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014 Karanraj 3 | All rights reserved. 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions 6 | are met: 7 | 1. Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 2. Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | 3. The name of the author may not be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR 15 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | /** 27 | @Description: Visualforce controller for the page to export the records directly from list view 28 | @Author: Karanraj(@karanrajs) 29 | **/ 30 | 31 | public with sharing class listviewAPI { 32 | 33 | public List fields {get;set;} 34 | public List columnName {get;set;} 35 | public List recordList {get;set;} 36 | public List> allRecords {get;set;} 37 | public List parserCol{get;set;} 38 | public String fileName {get;set;} 39 | public List columns; 40 | 41 | public void fetchListviewRecords() { 42 | 43 | columnName = new List(); 44 | fields = new List(); 45 | parserCol = new List(); 46 | recordList = new List(); 47 | allRecords = new List>(); 48 | try{ 49 | String listid = apexpages.currentpage().getparameters().get('listid'); 50 | String ObjectName = apexpages.currentpage().getparameters().get('Object'); 51 | String listName = apexpages.currentpage().getparameters().get('listName'); 52 | fileName = ObjectName +'_'+listName.replace(' ','_') +'_'+ Datetime.now().format(); 53 | //Http callout 54 | HttpRequest req = new HttpRequest(); 55 | req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); 56 | req.setHeader('Content-Type', 'application/json'); 57 | String domainUrl=URL.getSalesforceBaseUrl().toExternalForm(); 58 | system.debug('********domainUrl:'+domainUrl); 59 | String endpointUrl = domainUrl+'/services/data/v32.0/sobjects/' +(ObjectName=='Person_Account'?'Account':ObjectName)+'/listviews/'+listid+'/describe'; 60 | system.debug('domain URL' + endpointUrl); 61 | req.setEndpoint(endpointUrl); 62 | req.setMethod('GET'); 63 | Http h = new Http(); 64 | HttpResponse res = h.send(req); 65 | Map root = (Map)JSON.deserializeUntyped(res.getBody()); 66 | //Nested list logic to overcome collection limit 67 | for(Sobject sobj : Database.query((string)root.get('query'))){ 68 | recordList.add(sobj); 69 | if(recordList.size() == 10000){ 70 | allRecords.add(recordList); 71 | recordList = new List(); 72 | } 73 | } 74 | 75 | if(recordList != null && !recordList.isEmpty()) 76 | allRecords.add(recordList); 77 | 78 | //Parsing JSON string to get the column details 79 | JSONParser parser = JSON.createParser(res.getBody()); 80 | while (parser.nextToken() != null){ 81 | if(parser.getCurrentToken() == JSONToken.START_ARRAY) { 82 | while (parser.nextToken() != null) { 83 | if(parser.getCurrentToken() == JSONToken.START_OBJECT) { 84 | listviewAPI.Columns le = (listviewAPI.Columns)parser.readValueAs(listviewAPI.Columns.class); 85 | parserCol.add(le); 86 | 87 | } 88 | } 89 | } 90 | } 91 | 92 | for(listviewAPI.Columns lc : parserCol){ 93 | if(lc.hidden == false && lc.fieldNameOrPath != Null){ 94 | fields.add(lc.fieldNameOrPath); 95 | columnName.add(lc.label); 96 | } 97 | } 98 | }catch(Exception ex){ 99 | Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'There is some problem occurred, verify object name in the custom button code"')); 100 | } 101 | 102 | } 103 | 104 | public class Columns { 105 | 106 | public String ascendingLabel; 107 | public String descendingLabel; 108 | public String fieldNameOrPath; 109 | public Boolean hidden; 110 | public String label; 111 | public String selectListItem; 112 | public String sortDirection; 113 | public Integer sortIndex; 114 | public Boolean sortable; 115 | public String type; 116 | } 117 | } --------------------------------------------------------------------------------