0

I am trying to export/download a salesforce to an excel template. The template should be formatted with design and columns similar to the columns in the report. The template is stored as an attachment on a cusObject__c. I am able to export it to excel with visualforce page just fine but I am having issues with formatting there. So I ventured into REST API world. I am very new to APIs and this is my first API class.

public class excelReport {

    public excelReport(){
        Http http = new http();
        HttpRequest request = new HttpRequest();

        request.setEndpoint(System.URL.getSalesforceBaseURL().toExternalForm() + '/services/data/v45.0/analytics/reports/00OG00000046KbV');
        request.setMethod('GET');    
        request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
        request.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');


        HttpResponse response = http.send(request);

        if(response.getStatusCode() == 200){
            Map<String, Object> results = (Map<String, Object>)
            JSON.deserializeUntyped(response.getBody());
            System.debug(results);
        }
    }
}

It have got the report to come back with values but I am not being able to put it in a certain excel file and automatically download.
I have tried:
https://developer.salesforce.com/docs/atlas.en-us.api_analytics.meta/api_analytics/sforce_analytics_rest_api_download_excel.htm

https://releasenotes.docs.salesforce.com/en-us/summer18/release-notes/rn_rd_reports_exportwithformatting.htm

Generate xlsx file from Apex

Salesforce REST API Call

Any help will be appreciated!

3
  • There's several large AppExchange packages that do this - DrawLoop, Conga, Apsona, etc. Interpreting and merging into Excel format isn't a small task in any language, and it's going to be more difficult (if even possible) in Apex than on a server somewhere.
    – David Reed
    Mar 26, 2019 at 19:24
  • @DavidReed We have used Conga before but I am trying to build something similar in-house. What would be best approach for it? Or any resources that you can share? Thanks! Mar 26, 2019 at 20:31
  • I'd go with Conga. Note they do it on their server, not in apex
    – cropredy
    Mar 26, 2019 at 22:37

1 Answer 1

0

To request Excel instead of JSON format, change the Accept value in the request header to application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.

request.setHeader('Accept', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.