How to package multiple attachments as ZIP and put them in record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2023 12:10 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2023 03:35 AM
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:
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.
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');
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2023 11:24 PM
@Riya Verma Hi,sorry, I can't find in SN How to create object JSZip.Can you explain it a little more clearly?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2024 10:39 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2023 12:23 AM