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
SrinuSrinu 

Report in an email with CSV attachment

Hello,

We have few reports in place and whenever user runs they are recieving the report details in email body. 

Can we send the details as an attachment?

Any suggestions how can we achieve this functionality by using any custom code?

Thanks,
Srinu
Isidore Jose 5Isidore Jose 5
Hi Srinu,

this is an age old issue with salesforce. 

Please check this Idea Exchange URL
https://success.salesforce.com/ideaView?id=08730000000Brb6AAC#

If you are ok with a thridparty solution. Please use

http://www.cloud4j.com/report-builder.html

Regards
Isidore


 
ManojSankaranManojSankaran
Hi Srinu,

Below is the code sample that will create a CSV File in Apex.
Follow these steps which may help you.
1 . Create a batch class and write your custom logic to create CSV File.


String generatedCSVFile =”;
List<String> queryFields = new List<String>{‘Id’,’Type’,’Status’};
String fileRow = ”;
for(Account company: [select id,type,status from account limit 1000]){
fileRow = ”;
fileRow = fileRow +’,’+ company.Id;
fileRow = fileRow +’,’+ company.Type;
fileRow = fileRow.replaceFirst(‘,’,”);
generatedCSVFile = generatedCSVFile + fileRow + ‘\n’;

}
Messaging.EmailFileAttachment csvAttachment = new Messaging.EmailFileAttachment();
Blob csvBlob = blob.valueOf(generatedCSVFile);
String csvName = ‘company details which doesn’t have members.csv’;
csvAttachment.setFileName(csvName);
csvAttachment.setBody(csvBlob);

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{‘Your Email Id’};
String subject = ‘company details which doesn’t have members CSV’;
email.setSubject(subject);
email.setToAddresses(toAddresses);
email.setPlainTextBody(‘company details which doesn’t have members CSV’);
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttachment});
Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});



Mark it as answer if it really helps you.

Thanks
Manoj S
Marie Bertaud 5Marie Bertaud 5
Hello,
You can send an email with the report as attachment in Excel format with the following code :
Report report = [SELECT  Id,DeveloperName FROM Report WHERE DeveloperName = 'Name'];

            ApexPages.PageReference reportPage = new ApexPages.PageReference('/'+report.Id+'?excel=1&isdtp=p1'); 

            //Generate Attachment 
            Messaging.EmailFileAttachment csvAttachment = new Messaging.EmailFileAttachment();
            csvAttachment.setFileName('Report.xls);
            csvAttachment.setBody(reportPage.getContent());
            csvAttachment.setContentType('application/vnd.ms-excel');

            //Generate Email
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[]{'test@test.com'};
            String subject = 'Your Subject';
            email.setSubject(subject);
            email.setToAddresses(toAddresses);
            email.setPlainTextBody('Your text here.');
            email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttachment});
            Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});