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
alexander yurpolskyalexander yurpolsky 

Custom Export Button

Hi all,
 
Is it possible to create a custom button implementing the "Export Details" functionality on Selected View?

To be in detail, in my Contact list view which lists contacts based on the Selected View (All Contacts, My Contacts, Recently Viewed..), is it possible to have a custom "Export" button which will export the selected contacts to local Excel file OR send the the data to excel online ? Basically I just want to create a custom "Export" button to export the selected contact details to my local machine/excel online . Any suggestion would be of really a great help guys 

Thanks in advance
Best Answer chosen by alexander yurpolsky
Alain CabonAlain Cabon
Save to CSV/Excel in visual force with UTF 8 chracters

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

Tested on IE8, Firefox, Chrome.
 
Excel appears to coerce the font to Arial Unicode MS 10pt which is a good font for UTF-8. 
<apex:page controller="MyCustomController" cache="true" contenttype="application/x-excel#filename.xls" showheader="false">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
	<apex:pageBlock >
	    <apex:pageBlockTable value="{!allRecords}" var="line" columns="3">  
          	<apex:column headerValue="Col A">{!line.fieldA}</apex:column>
          	<apex:column headerValue="Col B">{!line.fieldB}</apex:column>
          	<apex:column headerValue="Col C">{!line.fieldC}</apex:column>
		</apex:pageBlockTable>
	</apex:pageBlock>
</apex:page>

https://developer.salesforce.com/forums/?id=906F0000000962nIAA
 

All Answers

Alain CabonAlain Cabon
Hi,

The main problem is the default pagesize (20 by default) that needs an apex controller.

1) List Button  (create the Visualforce page + controller below at first)
User-added image

2) Add the button to the Account List View

User-added image

User-added image

Visualforce page: the standardController detects the context for the current list.
<apex:page id="myPage" standardController="Account" extensions="AccountPage" recordSetvar="accounts" contentType="application/vnd.ms-excel#SalesForceExport.xls" cache="true">
    <apex:pageBlock id="myPageBlock" title="Viewing Accounts" >       
        <apex:variable value="{!1}" var="rowNum"/>
        <apex:form id="myForm"> 
            <apex:pageBlockSection id="myBlockSection" title="Accounts">       
                <apex:pageBlockTable value="{!accounts}" var="acc"  >  
                    <apex:column >{!rowNum}
                        <apex:variable var="rowNum" value="{!rowNum+1}"/>
                    </apex:column>
                    <apex:column value="{!acc.name}"/>
                    <apex:column value="{!acc.owner.name}"/>
                     <apex:column value="{!acc.phone}"/>
                    <apex:column value="{!acc.type}"/>
                    <apex:column value="{!acc.parent.name}"/>
                    <apex:column value="{!acc.billingcity}"/>
                </apex:pageBlockTable>          
            </apex:pageBlockSection>
        </apex:form> 
    </apex:pageBlock>
</apex:page>
contentType="application/vnd.ms-excel#SalesForceExport.xls"  will do all the needed work.

A controller just to set the Page Size.
public class AccountPage {
    private ApexPages.StandardSetController standardController;
    
    public AccountPage(ApexPages.StandardSetController standardController){ 
        this.standardController = standardController;
        this.standardController.setPageSize(1000);
    }
}

Alain
Pradeep SinghPradeep Singh
Hi,
Please refer below code. You have to modify it according to your requirements but this will surely help you.
After developing the functionality, create a custom button of list Button type. User-added image
<apex:page standardController="Account" recordSetVar="Account" contentType="application/vnd.ms-excel#SalesForceExport.xls" extensions="standardSetController_Export">
    <apex:repeat value="{!accList}" var="acc">
        {!acc.name}---- {!acc.Rating}
    </apex:repeat>    
</apex:page>

-------------------------------------------------------
public class standardSetController_Export{
    
    ApexPages.StandardSetController setCon;
    public List<account> accList{get;set;}

    public standardSetController_Export(ApexPages.StandardSetController controller) {
        setCon = controller;
        accList = [select id,name,rating from Account where id in : setCon.getSelected() ];
        system.debug('====='+accList);
    }

    public integer getMySelectedSize() {
        return setCon.getSelected().size();
    }
    public integer getMyRecordsSize() {
        return setCon.getRecords().size();
    }

}

If this solves your issue, please mark it as best answer.
alexander yurpolskyalexander yurpolsky
I tried both options and it works partially:
  1. In both options some of the data is not readable. Something that seems to be related to encoding (my data are in English, Arabic and Hebrew). Pradeep Singh solution  - https://ibb.co/cJtJhS (https://ibb.co/gcvTGn) Alain Cabon solution  - https://ibb.co/gcvTGn
  2.  @Alain Cabon - when my user clicks the Export Data button it goes to another page (blank page ). can the user stay on the same page?
  3. @Pradeep Singh - The data exported to Excel  is without the column header? Why?

Thank you for your help and patience :)
 
Pradeep SinghPradeep Singh
Hi Alexander, you have to make some modification in the code. You should have to use the html table to get that in desired format. I have just used the values not the label , but you can use that.
Pradeep SinghPradeep Singh
You can use below code between page tag.
<apex:pageBlock>
        <apex:pageBlockTable value="{!accounts}" var="acc" border="1" >           
            <apex:column value="{!acc.name}"/>
            <apex:column value="{!acc.Rating}"/>
             <apex:column value="{!acc.Website}"/>
            <apex:column value="{!acc.type}"/>
        </apex:pageBlockTable> 
    </apex:pageBlock>

After downloading the file, open the file in notepad++ or any other text editor and check if you are getting the same problem of encoding. 
alexander yurpolskyalexander yurpolsky
@Thanks for the response. 

After downloading the file and opening it via Notepad I can see the records correctly.
But in Excel I see only strange signs. Is there a way to download the file with universal encoding or something similar?
Alain CabonAlain Cabon
How do you define the behavior of your button? What is your browser?

With Chrome, the export appears some seconds just at the bottom of the page like any other exports (I made a dozen of exports with the button above) The screen itself doesn't change at zero because there is no navigation at all here

Copy/paste all the definition of your work (fresh eyes could help to see obvious errors).
Alain CabonAlain Cabon
 The screen itself doesn't change at all. Zero change on the screen.
alexander yurpolskyalexander yurpolsky
@Alain Cabon -  I'm sorry my mistake! You are right and the page does not change.
But I still have an encodoing  problem  with some of  my records. Is there anything to do with it?
Alain CabonAlain Cabon
Save to CSV/Excel in visual force with UTF 8 chracters

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

Tested on IE8, Firefox, Chrome.
 
Excel appears to coerce the font to Arial Unicode MS 10pt which is a good font for UTF-8. 
<apex:page controller="MyCustomController" cache="true" contenttype="application/x-excel#filename.xls" showheader="false">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
	<apex:pageBlock >
	    <apex:pageBlockTable value="{!allRecords}" var="line" columns="3">  
          	<apex:column headerValue="Col A">{!line.fieldA}</apex:column>
          	<apex:column headerValue="Col B">{!line.fieldB}</apex:column>
          	<apex:column headerValue="Col C">{!line.fieldC}</apex:column>
		</apex:pageBlockTable>
	</apex:pageBlock>
</apex:page>

https://developer.salesforce.com/forums/?id=906F0000000962nIAA
 
This was selected as the best answer