Home > Blog > Sending Report Data as CSV and Html Body in Email

Sending Report Data as CSV and Html Body in Email

Overview – Blog holds the explanation of using getContent() method to get report data.
Problem – I came across the requirement of sending the report data to a user in an email having the report data in both HTML body of an email and as CSV attachment to an email. Using ApexPages.PageReference, either proper HTML body was not received or CSV attached could not get data in UTF-8 (Rich Text Format).
Solution – The possible solution is that while adding email attachment, the ApexPages.PageReference was called using CSV formatting and while creating HTML body, ApexPages.PageReference was called using EXCEL formatting. Explanation: – So, according to my requirement, I was asked to send the Report Data in both HTML body of an email and as CSV attachment to that same email. Well, it was a bit tricky to think, but I came across this idea of using two calls. One for adding HTML Body using ApexPages.PageReference called in Excel formatting and other calls for adding CSV attachment to the same email. CSV Error :   HTML Body Error: Here are the few lines of code that made the trick: Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); ApexPages.PageReference report = new ApexPages.PageReference( ‘/’+reportId[i]+’?excel=1′); Blob content = report.getContent(); message.setHtmlBody(content.toString());   Above code will get report data based on “reportId[i]”  which is basically an array of “STRINGS” and will pass on “Report Id” to the parameters of ApexPages.PageReference. Using basic URL Hacks, we can convert this PageReference to an “EXCEL” by adding “?excel=1’” to the URL. After that, all we need to do is to call “getContent()” method, which will convert the excel to BLOB type. Thereafter in setHtmlBody(), we convert the BLOB to Strings which in turn is displayed as HTML in the email body. Now, onto sending CSV to the same email. Follow some lines of code: sampleMethod1(){ Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message. setFileAttachments(sampleMethod2){ } List< Messaging.EmailFileAttachment> sampleMethod2(){ Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); List< Messaging.EmailFileAttachment> lstAttachment = new List< Messaging.EmailFileAttachment>(); List <Report> reportList = [SELECT Id,Name,DeveloperName FROM Report where Name = ‘Sample Report’]; System.debug(reportList); String[] reportId = new String[reportList.size()]; for(Integer i=0;i < reportList.size();i++) { reportId[i] = (String)reportList.get(i).get(‘Id’); ApexPages.PageReference report = new ApexPages.PageReference( ‘/’ + reportId[i] + ‘?csv=1’); Blob content = report.getContent(); attachment.setBody(content); attachment.setContentType(‘text/csv’); lstAttachment.add(attachment); } return lstAttachment; } For adding CSV attachment, the trick is to call a method returning “List< Messaging.EmailFileAttachment>” type value. For sending report data in CSV, we call the Report using “ApexPages.PageReference” and passing ID using “reportId[i]” and then using URL Hacks, we can convert this PageReference to a “CSV” by adding “?csv=1’” to the URL. Because if we have used EXCEL formatting here in attachment, then the getContent() faces the problem of converting BLOB to proper UTF-8 in CSV file. Then we need to call “getContent()” which returns a BLOB type value. This BLOB type value is then passed into “attachment.setBody()”. We also need to set the “contentType” of attachment to “text/CSV”. Test Class Coverage: There was also an issue for covering “getContent” call. Error received was “Test method does not support getContent call” So to cover the “getContent” call, all we need to do is where the “getContent” is called in code, do the following in your code: if(!Test.isRunningTest()){ content = report.getContent(); }else { content = Blob.ValueOf(‘Test’); } This will prevent code control from entering inside if block. Hence the getContent() method will not get called. Conclusion: – Hence, to send report data using getContent() method we need to make two different separate calls to ApexPages.PageReference.  
Enquire Now

lets get over a cup of coffee and discuss!

Page Name: