Catalog item attachment send as zip file

sukran
Mega Sage

How to send ritm attachment as zip file to the email of the requestor?

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, starting with Tokyo release Flow designed provides a ZIp functionality,

ZIP step (servicenow.com)

So you should be able to create a flow or subflow that you can use to zip your RITM attachment and then email to your user.

Amit Gujarathi
Giga Sage
Giga Sage

HI @sukran ,
I trust you are doing great.

  1. Retrieve the RITM record using the unique identifier (sys_id) or any other suitable query.
  2. Get the email address of the requestor associated with the RITM.
  3. Create a zip file containing the attachment file(s) from the RITM record.
  4. Attach the zip file to an email and send it to the requestor's email address.

Here's an example code snippet to accomplish this in ServiceNow using server-side scripting (such as in a Business Rule or Script Include):

 

// Step 1: Retrieve the RITM record
var ritmSysId = '<RITM_sys_id>'; // Replace with the actual RITM sys_id
var ritmGr = new GlideRecord('sc_req_item');
if (ritmGr.get(ritmSysId)) {
  
  // Step 2: Get the requestor's email address
  var requestorEmail = ritmGr.variables.requested_for.email.toString();
  
  // Step 3: Create a zip file containing the attachment(s)
  var attachments = [];
  var attachmentGr = new GlideRecord('sys_attachment');
  attachmentGr.addQuery('table_sys_id', ritmSysId);
  attachmentGr.query();
  while (attachmentGr.next()) {
    attachments.push(attachmentGr);
  }
  
  // Step 4: Attach the zip file and send the email
  var zipFile = new GlideZip();
  for (var i = 0; i < attachments.length; i++) {
    var attachment = attachments[i];
    var fileContent = new GlideSysAttachment().getContent(attachment);
    zipFile.addBase64TextFile(attachment.file_name, fileContent);
  }
  
  var email = new GlideEmailOutbound();
  email.setSubject('Attachment(s) from your Requested Item');
  email.setBody('Please find the attachment(s) requested.');
  email.addRecipient(requestorEmail);
  email.addAttachment(zipFile.getBytes(), 'attachments.zip');
  email.send();
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



sukran
Mega Sage

Awesome @Amit Gujarathi 

sukran
Mega Sage

@Amit Gujarathi 

new GlideZip() is not working , Any idea ?