Pull attachment script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2024 12:02 AM - edited 10-08-2024 04:11 AM
Hi,
Quite similar to this thread: Pull watch list emails script I need to pull an attachment from an incident, if the attachment exist, and add it to a Email Client Template to be used for Incident Communication Tasks. The attachment is always called 'Problemrapport.docx' and I thought that I could just fetch that attachment via a Script Include and call that Script Include from the Email Client Template, but it does not seem to work. I am not sure where to call the Script Include from on the template either since it has not scripted fields. Should it work from any field (Cc or Bcc fields for instance) ?
Script name: getAttachmentsFromIncident
var getAttachmentsFromIncident = Class.create();
getAttachmentsFromIncident.prototype = {
initialize: function() {},
attachSpecificFile: function(email, incId, fileName) {
var attachmentGR = new GlideRecord('sys_attachment');
// Query to get attachments related to the incident with the specific file name
attachmentGR.addQuery('table_name', 'incident');
attachmentGR.addQuery('table_sys_id', incId); // The sys_id of the incident
attachmentGR.addQuery('file_name', fileName); // The specific file name
attachmentGR.query();
// Check if attachment exists and attach it to the email
if (attachmentGR.next()) {
// Attach the file to the email
email.addAttachment(attachmentGR);
gs.log('Attachment found and added to email: ' + fileName);
return true; // Indicate that the attachment was added
} else {
gs.log('No attachment found with the name: ' + fileName);
}
return false; // No attachment found
},
type: 'getAttachmentsFromIncident'
};
From the template I guess that this should work:
javascript: new getAttachmentsFromIncident().attachSpecificFile(email, current.incident_alert.source_incident.sys_id, 'Problemrapport.docx');
But alas is doesn't
Can you see why?
Best regards
Thomas

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2024 12:31 AM
Hi,
Suggest you create your script as "Notification Email Script". And then you can call this script from email templates using below syntax
${mail_script:<notification_email_script>}
Note: Replease <notification_email_script>with actual name
Palani

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2024 10:43 PM
Hello @Thomas G
Thank you for marking my response as helpful. Please accept my response as solution if you feel your question is anwered.
Thank you,
Palani
Palani

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2024 01:03 AM
Hello Palani,
Thanks for your help.
I have created an Email Script called 'getAttachmentsFromIncident' with this content:
// Ensure we are working with a valid incident sys_id from the incident_alert
var incidentSysId = current.incident_alert.source_incident.sys_id;
// Check if the incident sys_id is valid
if (incidentSysId) {
var attachmentGR = new GlideRecord('sys_attachment');
// Query to get attachments related to the incident with the specific file name
attachmentGR.addQuery('table_name', 'incident');
attachmentGR.addQuery('table_sys_id', incidentSysId);
attachmentGR.addQuery('file_name', 'Problemrapport.docx'); // Look for the specific attachment
attachmentGR.query();
// Check if the attachment exists
if (attachmentGR.next()) {
// Log the found attachment details
gs.log('Found attachment: ' + attachmentGR.file_name + ' for incident ' + incidentSysId);
// Use GlideSysAttachment to add the attachment to the email
var gsa = new GlideSysAttachment();
gsa.addAttachment(attachmentGR, email); // Directly attach to the email
gs.log('Attachment ' + attachmentGR.file_name + ' has been added to the email.');
} else {
gs.log('No attachment named Problemrapport.docx found for incident ' + incidentSysId);
}
} else {
gs.log('No valid incident sys_id found for incident_alert.');
}
In the HTML body of the Email Client Template I call it with this line:
${mail_script:getAttachmentsFromIncident}
Log shows that the attachment is found:
08-10-2024 10:00:55
Information Sent email 6741987a1b495610de0bed30604bcbbb for sys_users and sys_groups. EMAIL.6741987a1b495610de0bed30604bcbbb
08-10-2024 10:00:31
Information Found attachment: Problemrapport.docx for incident e9df54831b089a10de0bed30604bcbab *** Script
08-10-2024 10:00:31
Information Attachment Problemrapport.docx has been added to the email. *** Script
08-10-2024 10:00:31
Information Email client created email.
But alas there is nothing attached when I inspect the email record in the sys_email table and I can't figure out why.
Best regards
Thomas

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2024 04:08 AM
Hi,
I think, the below line may not work
// Use GlideSysAttachment to add the attachment to the email
var gsa = new GlideSysAttachment();
gsa.addAttachment(attachmentGR, email); // Directly attach to the email
Replace this line with following:
var gsa = new GlideSysAttachment().getContentStream(attachmentGR.sys_id);
new GlideSysAttachment().writeContentStream(current,attachmentGR.getValue('file_name'), attachmentGR.getValue('content_type'), gsa);
Palani