我有销售人员报告,我想要的是每天以csv格式安排此报告,以便用户通过此附件获取邮件。
答案 0 :(得分: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});
}
}
答案 1 :(得分:0)
这是我在Kevindotcar找到的解决方案:
http://kevindotcar.wix.com/home/apps/blog/how-to-schedule-a-report-to-send
涉及自定义对象,可视化实体页面,可视化实体电子邮件模板以及可调度类和工作流。看起来相当整洁,但没有太多代码。