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

@Ankur Bawiskar 

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..."

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

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

@Ankur Bawiskar 

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)?

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

@Ankur Bawiskar 

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.