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
sparc1.3696571782628958E12sparc1.3696571782628958E12 

Renaming a Report while exporting

Hi All

 

At present when I am exporting or taking a printable view of any report, the downloaded file is named as say for ex: "report1369745790672".

 

I want to give some meaningful name to it while exporting itself. I do not wish to manually rename it post its download.

 

Is it possible through apex?

 

Please help. URGENTLY REQUIRED.

 

Thanks

Shiv

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
<apex:page controller="exportReport" showHeader="false" showChat="false" contentType="application/vnd.ms-excel#{!$Request.fileName}"><apex:outputText escape="false" value="{!reportData}"/></apex:page>

 

public with sharing class exportReport {

    public String getReportData() {
        Id reportId = (Id)ApexPages.currentPage().getParameters().get('id');
        if(reportId != null && reportId.getSobjectType() == Report.SobjectType) {
            PageReference ref = new PageReference('/'+reportId+'?csv=1');
            System.debug(logginglevel.error,ref.geturl());
            return ref.getContent().toString();
        }
        return null;
    }

}

 Usage:

 

https://<server>.salesforce.com/apex/exportReport?id=<report.id>&fileName=<filename.ext>

You can change csv to excel to get a different view.

 

You'll still need a custom link to make this work, though, so you won't just be able to, say, export the report using the normal report button. You could make a bookmarklet that could allow a single-button export feature in most browsers.

 

For passing parameters to the report, you'll have to write extra code for that, too. This is just a demo.

All Answers

sfdcfoxsfdcfox
<apex:page controller="exportReport" showHeader="false" showChat="false" contentType="application/vnd.ms-excel#{!$Request.fileName}"><apex:outputText escape="false" value="{!reportData}"/></apex:page>

 

public with sharing class exportReport {

    public String getReportData() {
        Id reportId = (Id)ApexPages.currentPage().getParameters().get('id');
        if(reportId != null && reportId.getSobjectType() == Report.SobjectType) {
            PageReference ref = new PageReference('/'+reportId+'?csv=1');
            System.debug(logginglevel.error,ref.geturl());
            return ref.getContent().toString();
        }
        return null;
    }

}

 Usage:

 

https://<server>.salesforce.com/apex/exportReport?id=<report.id>&fileName=<filename.ext>

You can change csv to excel to get a different view.

 

You'll still need a custom link to make this work, though, so you won't just be able to, say, export the report using the normal report button. You could make a bookmarklet that could allow a single-button export feature in most browsers.

 

For passing parameters to the report, you'll have to write extra code for that, too. This is just a demo.

This was selected as the best answer
sparc1.3696571782628958E12sparc1.3696571782628958E12

Hi Fox

 

First of all thanx for sharing the code.

 

The thing is I have got multiple reports to export at the click of one custom button.

 

Even if I create such dynamic URLs (as mentioned in your code above) for different reports, can you please help/explain as to how do I loop through these URLs and get them saved all at once. AT THE GO OF ONE BUTTON CLICK.

 

Thanks in advance

Shiv 

sfdcfoxsfdcfox
You could loop through them in the controller, using getContent() for each, and then concatenating them together. Note that the entire page will be saved as ONE file regardless of the number of entries in the source. This might be a limitation for you. It may be that you're looking for something custom instead of just creating reports.
Robert Davis 1Robert Davis 1
I have the following test code for the above that works ( I did not write it but it worked for me). Sharing to help out someone else:
@isTest 
public class reporttest
{
       @isTest(SeeAllData='true') 
       public  static void testMethod1() 
    {
      List <Report> reportList = [SELECT Id,DeveloperName FROM Report where
          DeveloperName = 'X60_Day_ATR_Activity'];
      String reportId = (String)reportList.get(0).get('Id');
  
      PageReference pageRef = Page.exportReportVFP;

        Test.StartTest(); 
			Test.setCurrentPage(pageRef);
   			pageRef.getParameters().put('id',reportId );
            exportReportController tgetReportData= new exportReportController();
            tgetReportData.getReportData();
        Test.StopTest();
    }
}
#Ohana