- 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-10-2022 12:27 AM
Hi,
so it's coming in single line?
can you share how it's coming?
this should work fine
fileNameArr.join('\n')
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-10-2022 12:44 AM
Hi Ankur,
It does not print the text "One or more attachments" in the work notes of the ticket.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-10-2022 12:47 AM
Can you share how it added by sharing screenshot?
in your earlier response you mentioned it worked but showed the file names in comma
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-11-2022 05:57 AM
Hi
The team has decided not to go further with separating the filenames with a new line. We will use comma instead. This is the working script including the gs.info you suggested.
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.getValue());
}
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(', ');
gr.update();
}
}
I have another issue with the above script.
It is working if all the files attached in the email are discarded. If I attach 2 or more files and at least 1 file is discarded, it does not print to the work notes. The gs.info Row count is also not captured in the system log.
Do you have an idea why this is happening?
Please help!
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-11-2022 06:00 AM
Hi,
It should work even for 1 file
I believe I have already answered your question
Would you mind marking my response as correct and close the thread.
Regards
Ankur
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader