How to update script to get filename of attached file

ceraulo
Mega Guru

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.

1 ACCEPTED SOLUTION

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

16 REPLIES 16

Thanks @Ankur Bawiskar 

I'll create another thread with this other issue.

Cheers!

discussion can continue in this thread as well

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader