Apex · Salesforce

How to export(CSV/Excel) Salesforce Reports using Apex and send email

There are many case – user want to review data weekly or monthly base but they want to data as CSV/Excel file in the Email.

Many of now writing apex batch job to generate CSV fie and sending email. There is easiest way to use Salesforce Report ID and export in the Apex code.

Screen Shot 2017-04-11 at 10.32.11 AM

Sample Code for Excel:

ApexPages.PageReference objPage = new ApexPages.PageReference('/00O90000008oZK1?excel=1');

Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();

objMsgEmailAttach.setFileName('Testing.xls');

objMsgEmailAttach.setBody(objPage.getContent());

objMsgEmailAttach.setContentType('application/vnd.ms-excel');

 

Sample Code for CSV:

ApexPages.PageReference objPage = new ApexPages.PageReference('/00O90000008oZK1?csv=1');

Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();

objMsgEmailAttach.setFileName('Testing.csv');

objMsgEmailAttach.setBody(objPage.getContent());

objMsgEmailAttach.setContentType('text/csv');

 

 

 

 

2 thoughts on “How to export(CSV/Excel) Salesforce Reports using Apex and send email

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s