function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
marys pindamarys pinda 

Create a zip file in Apex

Hello,
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,
Sonam_SFDCSonam_SFDC
closeddate=TODAY should work.

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!
marys pindamarys pinda
Thank you my friend!

I read this article before . I'll try to make the integration with my code!

Thank you, bye
Arif SyedArif Syed
Hi, did you manage to get the the file ziped before sending out an email out as I am working on a similar requirment. 

Thanks,
Pedro I Dal ColPedro I Dal Col
Hi Marys,

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.
 
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;
        }
        
        //Create a new zip file and add cases.csv to it
        Zippex zip = new Zippex();
        zip.addFile('cases.csv', Blob.valueOf(finalStr), null);
        Blob zipBlob = zip.getZipArchive();
        
        Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
        String zipName = 'cases.zip';
        csvAttc.setFileName(zipName);
        csvAttc.setBody(zipBlob);
        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});
    }
}