How to check attachments in sys_attachments from catalog item (client script)??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-08-2020 08:53 AM
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)
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???????
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-08-2020 09:07 AM
Hi,
Check this out:
Thanks,
Ashutosh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-08-2020 09:36 AM
Hi
refer the following thread it might help you.
If it help mark helpful or correct
Thanks and regards
Anil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-09-2020 09:41 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-09-2020 12:17 PM
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