4

我有一份销售人员报告,我想要每天以 csv 格式安排此报告,以便用户收到带有此附件的邮件。

4

2 回答 2

7

您可以使用以下代码安排报告

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> {'test@salesforce.com'};
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});
  }
}
于 2013-03-21T08:08:39.800 回答
0

这是我由 Kevindotcar 找到的解决方案:

http://kevindotcar.wix.com/home/apps/blog/how-to-schedule-a-report-to-send

涉及自定义对象、visualforce 页面、visualforce 电子邮件模板以及可调度类和工作流。虽然看起来相当整洁,没有太多的代码。

于 2013-09-16T10:21:31.127 回答