Reputation: 3126
I have a sales force report and what I want is to schedule this report in csv format on daily basis so that the user gets mail with this attachment.
Upvotes: 4
Views: 3257
Reputation: 23064
Here's a solution I found by Kevindotcar:
http://kevindotcar.wix.com/home/apps/blog/how-to-schedule-a-report-to-send
Involves a custom object, a visualforce page, a visualforce email template and a schedulable class and a workflow. Seems fairly neat though, not too much code.
Upvotes: 0
Reputation: 326
You can schedule the report using the following code
global class ExporterCSV implements System.Schedulable {
global void execute(SchedulableContext sc) {
List<Merchandise__c> acclist = [Select id , name , CreatedDate , lastModifiedDate from Merchandise__c limit 10];
string header = 'Record Id , Name , Created Date , Modified Date \n';
string finalstr = header ;
for(Merchandise__c a: acclist)
{
string recordString = a.id + ',' + a.Name + ',' + a.CreatedDate + ',' + a.LastModifiedDate + '\n';
finalstr = finalstr + recordString;
}
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(finalstr);
string csvname= 'Invoice.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {'[email protected]'};
String subject = 'Merchandise Report CSV';
email.setSubject(subject);
email.setToAddresses( toAddresses );
email.setPlainTextBody('The Merchandise report is attached here.');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
Upvotes: 7