Notification email script
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2025 06:29 AM
Hi All,
In change request we are attaching different files. When we trigger the notification on change request table only XL file should be copied to email and ignore other files. We tried email script which is not working.
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id',current.sys_id);
attachment.addQuery("table_name", "change_request");
attachment.query();
while (attachment.next()) {
var fileName = attachment.file_name.toString();
gs.log("Attachment found: " + fileName);
var lowerFileName = fileName.toLowerCase();
gs.log("Lower file name: " + lowerFileName);
var isExcel = lowerFileName.endsWith('.xls') || lowerFileName.endsWith('.xlsx');
gs.log(".xls check: " + lowerFileName.endsWith('.xls'));
gs.log(".xlsx check: " + lowerFileName.endsWith('.xlsx'));
gs.log("Is Excel: " + isExcel);
if (isExcel) {
var copyResult = GlideSysAttachment.copy(attachment.table_name, attachment.table_sys_id, 'sys_email', current.sys_id);
gs.log("Copy result: " + copyResult);
} else {
gs.log("Non-Excel attachment skipped: " + fileName);
}
}
})(current, template, email, email_action, event);
Thanks in advance.
Labels:
- Labels:
-
Change Management
2 REPLIES 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2025 07:44 AM
@Anoja What is the error you getting, Can you try with below script:
(function runMailScript(current, template, email, email_action, event) {
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', current.sys_id);
attachment.addQuery("table_name", "change_request");
attachment.query();
while (attachment.next()) {
var fileName = attachment.file_name.toString();
var lowerFileName = fileName.toLowerCase();
var isExcel = lowerFileName.endsWith('.xls') || lowerFileName.endsWith('.xlsx');
if (isExcel) {
gs.log("Copying Excel file: " + fileName);
// Read file contents
var fileData = new GlideSysAttachment().getBytes(attachment);
if (fileData) {
// Attach to the email
new GlideSysAttachment().write(email, fileName, attachment.content_type, fileData);
gs.log("Successfully attached Excel file to email: " + fileName);
} else {
gs.log("Failed to read file data: " + fileName);
}
} else {
gs.log("Skipping non-Excel attachment: " + fileName);
}
}
})(current, template, email, email_action, event);
Please mark correct/Helpful if this helps you
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2025 05:13 AM
If you found this helpful, please accept as solution. Thanks