How to package multiple attachments as ZIP and put them in record

Echo1
Tera Contributor

hi all,

 

When I send an email, I will send it with the attachment of case, but too many attachments will lead to the incomplete attachment. I wish there was a way to package the attachments I need to send into a zip file and send them. So I would like to know how to package attachments into zip files through scripts.

 

thanks, thanks, thanks.

7 REPLIES 7

Riya Verma
Kilo Sage
Kilo Sage

Hi @Echo1 ,

Hope you are doing great.

 

To package attachments into a zip file and send them via email using ServiceNow, you can utilize script-based solutions:

  1. Identify the attachments you want to include in the zip file. You can retrieve these attachments using the ServiceNow APIs or by querying the appropriate tables.

  2. Once you have the list of attachments, create a script to generate the zip file. ServiceNow provides the GlideSysAttachment API, which you can use to retrieve attachment data and package them into a zip file.

Please find below script for reference :

 

// Retrieve the attachments based on your criteria (e.g., related case or specific conditions)
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'your_table_name');
attachmentGR.addQuery('table_sys_id', 'your_table_sys_id');
attachmentGR.query();

// Create a zip file using the JSZip library (include the library in your ServiceNow instance)
var zip = new JSZip();

// Iterate through each attachment record
while (attachmentGR.next()) {
  var file = new GlideSysAttachment().getContentStream(attachmentGR);
  var fileName = attachmentGR.getValue('file_name');
  
  // Add the attachment file to the zip
  zip.file(fileName, file);
}

// Generate the zip file as a base64-encoded string
var zipData = zip.generate({ type: 'base64' });

// Send the zip file as an email attachment
var email = new GlideEmailOutbound();
email.setSubject('Your email subject');
email.setBody('Your email body');
email.addAttachment('attachment.zip', zipData, 'application/zip');
email.send('recipient@example.com');

 

 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Echo1
Tera Contributor

@Riya Verma Hi,sorry, I can't find in SN How to create object JSZip.Can you explain it a little more clearly?

Hello,

 

How do I include a library in my servicenow instance? I've downloaded the JSZip library ( https://stuk.github.io/jszip/ ), and I've searched for a step by step to include it on my instance, but I couldn't find it.

 

I would really appreciate the help, I've never done this before, and I'm pretty clueless 😧

 

Best Regards,

Nathalia

Ankur Bawiskar
Tera Patron
Tera Patron

@Echo1 

flow designer has this step out of the box. check that and implement it

ZIP step 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader