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

Hi,

so it's coming in single line?

can you share how it's coming?

this should work fine

fileNameArr.join('\n')

Regards
Ankur

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

Hi Ankur,

It does not print the text "One or more attachments" in the work notes of the ticket.

Can you share how it added by sharing screenshot?

in your earlier response you mentioned it worked but showed the file names in comma

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

Hi @Ankur Bawiskar 

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.

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

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