How to check attachments in sys_attachments from catalog item (client script)??

Matt Cordero1
Tera Guru

I am trying to query the sys_attachments table using GlideAjax in a client script to see if a Request Item has attachments, before submission.

My issue is that I can see that I have attachments returning back (in the response), but I can't seem to get the "filename" to appear. (see screenshot)

find_real_file.png

Any ideas???????

Here is the client side script:

var message = 'Please attach the most current price sheet.';

function onSubmit() {	
	var pluc = g_form.getValue('plu_creation');
	var pluu = g_form.getValue('plu_pricing_update');
	
	if (pluc == 'true' || pluu == 'true') {	
		try {  
			// for non-portal view		
			var attachments = document.getElementById('header_attachment_list_label');
			if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
				g_form.addErrorMessage(message);
				return false;
			}
			
			var table_sys_id = gel('sysparm_item_guid').value;
			
			// query 'sys_attachment' table with GlideAjax
			var ga = new GlideAjax('GetAttachments');
			ga.addParam('sysparm_name', 'getAttachments');
			ga.addParam('sysparm_table_name', 'sc_cart_item');
			ga.addParam('sysparm_table_sys_id', table_sys_id);
			ga.getXML(getAttachments);
			
			return false;
		}
		catch (e) { 
			// for Service Portal
			var count = getSCAttachmentCount();
			if(count <= 0) {
				g_form.addErrorMessage(message);
				return false;
			}
		}
	}
}

function getAttachments(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.addInfoMessage(answer); //JSON String

    answer = answer.evalJSON(); //Transform the JSON string to an object
    g_form.addInfoMessage(answer);

	var filename = "";
    for(var i = 0; i < answer.length; i++) { //Loop into the array
		filename = answer[i].filename;
		g_form.addInfoMessage("Filename: " + filename);
    }
}

Here is the script include:

var GetAttachments = Class.create();
GetAttachments.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getAttachments: function () {
		try {
			// Get parameter variables
			var table_name = this.getParameter('sysparm_table_name');
			var table_sys_id = this.getParameter('sysparm_table_sys_id');
			var attachmentsArray = [];
			
			// GlideRecord to attachments table
			var gr = new GlideRecord('sys_attachment');
			gr.addQuery('table_name', table_name);
			gr.addQuery('table_sys_id', table_sys_id);
			gr.query();
			while(gr.next())
			{
				var attachmentObject = {};
				attachmentObject.filename = gr.file_name;
				attachmentsArray.push(attachmentObject);
			}
			
			var json = new JSON();
			var data = json.encode(attachmentsArray); //JSON formatted string
			
			return data;
		}
		catch(ex) {
			var message = ex.message;			
			return 'ERROR: ' + message;
		}
	},
	
	type: 'GetAttachments'
});

Any ideas???????

6 REPLIES 6

Ashutosh Munot1
Kilo Patron
Kilo Patron

Matt Cordero1
Tera Guru

Ok, can anyone look at the posted code and determine WHY the Client Script shows Filename: [object Object] instead of the name of the file name???

find_real_file.png

Thank you.

HI,

Can you replace 

var data = json.encode(attachmentsArray);

With

var data = JSON.Stringify(attachmentsArray);

 

AND

answer = answer.evalJSON();

with

answer = JSON.parse(answer);
Thanks,
Ashutosh