You need to sign in to do that
Don't have an account?
marys pinda
Create a zip file in Apex
Hello,
I have an Apex class that generates a CSV file.
I wonder how do I put this CSV (cases.csv) within a ZIP and to send an email.
It is possible to do in the same class?
It's correct: ... where closeddate=TODAY ???
Thank you,
I have an Apex class that generates a CSV file.
global class AAAExporterCSV implements System.Schedulable { global void execute(SchedulableContext sc) { List<case> acclist = [Select casenumber,subject, accountid from case where closeddate=TODAY]; string header = 'Id, Subject,Account, '+'\n'; string finalstr = header ; for(case a: acclist) { string recordString = a.casenumber + ',' + a.subject+ ',' + a.accountid +'\n'; finalstr = finalstr + recordString; } Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment(); blob csvBlob = Blob.valueOf(finalstr); string csvname= 'cases.csv'; csvAttc.setFileName(csvname); csvAttc.setBody(csvBlob); Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage(); String[] toAddresses = new list<string> {'v.veve@veve.com'}; String subject = '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}); } }
I wonder how do I put this CSV (cases.csv) within a ZIP and to send an email.
It is possible to do in the same class?
It's correct: ... where closeddate=TODAY ???
Thank you,
Also, I did find a piece of code which you can use to zip a file - you would have to integrate that with your code and then use it to sent the zip as attachment:
http://www.valnavjo.com/blog/compressing-files-in-salesforce/
Worth a read!
I read this article before . I'll try to make the integration with my code!
Thank you, bye
Thanks,
Salesforce doesn't provide zip support out of the box but you can use Zippex which is an open source zip library for Apex. https://github.com/pdalcol/Zippex
I modified your code to add cases.csv inside a zip file before it is attached to the email message. Make sure to install Zippex from the link above before you try this code.