- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2022 04:05 AM
Hello!
I have a script action that adds a work note to the ticket when an attachment on an incoming email is discarded by ServiceNow. The next requirement is to get the file names of the discarded files and it to the work note. I tried updating my working script but it's not working as I intended.
var target_table = current.target_table;
var target = current.instance;
var gr = new GlideRecord(current.target_table);
gr.addQuery('sys_id', target);
gr.query();
if (gr.next()) {
var gr2 = new GlideRecord('sys_email_attachment');
gr2.addQuery('email.sys_id', target);
gr2.addQuery('action', 'discarded');
gr2.query();
if (gr2.next()) {
if (target_table == "incident" || target_table == 'sc_task') {
gr.work_notes = "One or more attachments from an email sent by " + current.user_id.name + " with subject " + current.subject + " were discarded. " + gr2.file_name;
gr.update();
}
}
Please help!
Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2022 01:13 AM
Hi,
yes like this
var target_table = current.target_table;
var target = current.instance;
var sysId = current.sys_id;
var gr = new GlideRecord(current.target_table);
gr.addQuery('sys_id', target);
gr.query();
if (gr.next()) {
var fileNameArr = [];
var gr2 = new GlideRecord('sys_email_attachment');
gr2.addQuery('email.sys_id', sysId);
gr2.addQuery('action', 'discarded');
gr2.query();
while(gr2.next()) {
fileNameArr.push(gr2.getValue('file_name'));
}
if (target_table == "incident" || target_table == 'sc_task') {
gr.work_notes = "One or more attachments from an email sent by " + current.user_id.name + " with subject " + current.subject + " were discarded. " + fileNameArr.join('\n');
gr.update();
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2022 11:14 PM
I stand corrected. When I tested your updated script, the Work Notes was not updated with the text "One or more attachments from an email sent by..."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2022 12:39 AM
Hi,
add gs.info and then in system logs
var target_table = current.target_table;
var target = current.instance;
var sysId = current.sys_id;
var gr = new GlideRecord(current.target_table);
gr.addQuery('sys_id', target);
gr.query();
if (gr.next()) {
var fileNameArr = [];
var gr2 = new GlideRecord('sys_email_attachment');
gr2.addQuery('email.sys_id', sysId);
gr2.addQuery('action', 'discarded');
gr2.query();
gs.info('Row count' + gr2.getRowCount());
while(gr2.next()) {
fileNameArr.push(gr2.file_name.getDisplayValue());
}
if (target_table == "incident" || target_table == 'sc_task') {
gr.work_notes = "One or more attachments from an email sent by " + current.user_id.name + " with subject " + current.subject + " were discarded. " + fileNameArr.toString();
gr.update();
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2022 01:00 AM
I was able to make it work using the below script.
var target_table = current.target_table;
var target = current.instance;
var sysId = current.sys_id;
var gr = new GlideRecord(current.target_table);
gr.addQuery('sys_id', target);
gr.query();
if (gr.next()) {
var fileNameArr = [];
var gr2 = new GlideRecord('sys_email_attachment');
gr2.addQuery('email.sys_id', sysId);
gr2.addQuery('action', 'discarded');
gr2.query();
while(gr2.next()) {
fileNameArr.push(gr2.getValue('file_name'));
}
if (target_table == "incident" || target_table == 'sc_task') {
gr.work_notes = "One or more attachments from an email sent by " + current.user_id.name + " with subject " + current.subject + " were discarded. " + fileNameArr.toString();
gr.update();
}
}
When printed in the Work Notes, the filenames are separated by a comma. Is it possible to display the filenames as a list (in different rows)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2022 01:13 AM
Hi,
yes like this
var target_table = current.target_table;
var target = current.instance;
var sysId = current.sys_id;
var gr = new GlideRecord(current.target_table);
gr.addQuery('sys_id', target);
gr.query();
if (gr.next()) {
var fileNameArr = [];
var gr2 = new GlideRecord('sys_email_attachment');
gr2.addQuery('email.sys_id', sysId);
gr2.addQuery('action', 'discarded');
gr2.query();
while(gr2.next()) {
fileNameArr.push(gr2.getValue('file_name'));
}
if (target_table == "incident" || target_table == 'sc_task') {
gr.work_notes = "One or more attachments from an email sent by " + current.user_id.name + " with subject " + current.subject + " were discarded. " + fileNameArr.join('\n');
gr.update();
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2022 11:46 PM
This did not work when I tried your suggestion. I also tried fileNameArr.join(', ') but it did not work. Any idea why this does not work?
Thank you.