- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 12:35 PM
Hello,
A user replies to an existing sctask email notification with an attachment. The inbound email is added email body text to the correct record, but the attachments are not getting attach. Below is the script I'm currently using. Could someone please provide suggestion on how to get it to works. Thank you
// Extract the SCTASK number from the email subject
var taskNumber = email.subject.match(/SCTASK\d+/);
if (taskNumber) {
// Query the Task table for the matching SCTASK record
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('number', taskNumber[0]);
taskGR.query();
if (taskGR.next()) {
// Add email body to the Work Notes or Comments
// taskGR.comments = email.body_text;
taskGR.work_notes = email.body_text;
// Attach files from the email to the SCTASK
var attachment = new GlideSysAttachment();
attachment.copy('sys_email', email.sys_id, 'sc_task', taskGR.sys_id);
// Update the SCTASK record
taskGR.update();
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 06:39 PM - edited 12-26-2024 06:50 PM
are you saying user had attached some file such as word, pdf etc and it's not getting attached?
your code should attach it to target record
update as this, attachment copy should be after the update without which you won't get sysId of record
// Extract the SCTASK number from the email subject
var taskNumber = email.subject.match(/SCTASK\d+/);
if (taskNumber) {
// Query the Task table for the matching SCTASK record
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('number', taskNumber[0]);
taskGR.query();
if (taskGR.next()) {
// Add email body to the Work Notes or Comments
// taskGR.comments = email.body_text;
taskGR.work_notes = email.body_text;
// Attach files from the email to the SCTASK
var attachment = new GlideSysAttachment();
// Update the SCTASK record
taskGR.update();
attachment.copy('sys_email', sys_email.sys_id, 'sc_task', taskGR.sys_id);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 06:34 PM
Hello,
I successfully retrieved the email response with the attached file, which has been linked to the existing SCTASK record. However, the attachment is in the ".eml" format, and I was unable to obtain the original email in the ".msg" format. Any help would be appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 06:39 PM - edited 12-26-2024 06:50 PM
are you saying user had attached some file such as word, pdf etc and it's not getting attached?
your code should attach it to target record
update as this, attachment copy should be after the update without which you won't get sysId of record
// Extract the SCTASK number from the email subject
var taskNumber = email.subject.match(/SCTASK\d+/);
if (taskNumber) {
// Query the Task table for the matching SCTASK record
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('number', taskNumber[0]);
taskGR.query();
if (taskGR.next()) {
// Add email body to the Work Notes or Comments
// taskGR.comments = email.body_text;
taskGR.work_notes = email.body_text;
// Attach files from the email to the SCTASK
var attachment = new GlideSysAttachment();
// Update the SCTASK record
taskGR.update();
attachment.copy('sys_email', sys_email.sys_id, 'sc_task', taskGR.sys_id);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2024 08:38 AM
Good morning @Ankur Bawiskar ,
Please see my response below. Thank you so much for your help!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 06:45 PM
Hello Erica,
You can try making a change in the line with attachment.copy
. Instead of using email.sys_id
, replace it with sys_email.sys_id
and check if it works. According to the documentation, attachments should ideally be copied to the target records. For reference, see ServiceNow Inbound Email Actions Documentation.
attachment.copy('sys_email', sys_email.sys_id, 'sc_task', taskGR.sys_id);