Get attachment file name

Shuang
Tera Contributor

Hi Everyone,

I'm trying to get the attachment file name in a ticket, and write the file name into the worknote area.

I write my code as below.

I can file the attachment recoed,but don't know how to get the file name in that record.
Hope someone can help me to solve this problem.

Thank you very much in advanced.

Code:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if(newValue=='true'&& g_form.getValue('u_category')=='378ba2bbdbe697c0fc0792d8db9619a2'){
var id = g_form.getUniqueValue();
g_form.setMandatory('work_notes',true);

var att = new GlideRecord('sys_attachment');
att.addQuery('table_name','sc_request');
att.addQuery('table_sys_id',id);
att.query();
while (att.next()){

g_form.setValue('work_notes',g_form.getValue('work_notes')++'123');
}
}
if(newValue=='false'&& g_form.getValue('u_category')=='378ba2bbdbe697c0fc0792d8db9619a2'){
g_form.setMandatory('work_notes',false);
g_form.setValue('work_notes',' ');
//Type appropriate comment here, and begin script below

}

5 REPLIES 5

Hitoshi Ozawa
Giga Sage
Giga Sage

ServiceNow now doesn't recommend using GlideRecord() in client script. Use GlideAjax() to call script include script that fetches the filename.

niharika19
Mega Guru
Mega Guru

@Shuang 

 

Firstly GlideRecord() in client script is not a best practice instead you can use Glide Ajax .

Also The setValue() method is not supported for journal fields. Instead, assign values in script.

https://docs.servicenow.com/bundle/paris-platform-administration/page/administer/field-administratio...

 

I recommend to use a script include for fecting file name and also to enter the filename in worknotes .

 

Let me know incase anything else needed on this .

 

Thanks 

Hitoshi Ozawa
Giga Sage
Giga Sage

Not sure the requirements but the following code will add work notes to an incident.  Note, the onChange variable is the variable with the incident number. The script will add a work note to the incident with the entered incident number. 

May be, it would be better to create a business rule instead of a client script?

Client script

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

    var ajax = new GlideAjax('GetAttachmentFilename');
    ajax.addParam('sysparm_name', 'getFilename');
    ajax.addParam('sysparm_number', newValue);
	alert(newValue);
    ajax.getXMLAnswer(function(answer) {
        if (answer.length > 0) {
			alert(answer);
        }
    });
   
}

 

Script include:

var GetAttachmentFilename = Class.create();
GetAttachmentFilename.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getFilename: function() {
        var filenames = [];
        var number = this.getParameter('sysparm_number');

        var grInc = new GlideRecord('incident');
        if (grInc.get('number', number)) {
            var grAtt = new GlideRecord('sys_attachment');
			grAtt.addQuery('table_sys_id', grInc.sys_id);
			grAtt.query();
            while (grAtt.next()) {
                filenames.push(grAtt.file_name.toString());
				grInc.work_notes = grAtt.file_name.toString();
            }
			grInc.update();
			
        }
        return JSON.stringify(filenames);
    },

    type: 'GetAttachmentFilename'
});

Sample of incident with work notes entered via script.

find_real_file.png

I have a story table, in the record i have added new field , can we update attachment name in that field.

Could you please help me, what are the changes we need to modify in the above code