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-09-2020 02:37 PM
Ok, here is the modified client script. At this point, I am getting the file names returned from the server script (that works), but the callback function isn't stopping the form submission from happening. I need the validation to occur and stop the form from submitting if the Request Item doesn't have valid attachments.
Client 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 {
// START: Query 'sys_attachment' table and verify 'Pasted image.png' is not counted as an attachment
var table_sys_id = gel('sysparm_item_guid').value;
var table_name = 'sc_cart_item';
var ga = new GlideAjax('GetAttachments');
ga.addParam('sysparm_name', 'getAttachments');
ga.addParam('sysparm_table_name', table_name);
ga.addParam('sysparm_table_sys_id', table_sys_id);
ga.getXML(getAttachments);
// END: Query 'sys_attachment' table and verify 'Pasted image.png' is not counted as an attachment
}
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");
var attachments = JSON.parse(answer);
var file_name = "";
var hasValidAttachments = false;
for(var i = 0; i < attachments.length; i++) { //Loop into the array
file_name = attachments[i];
if(file_name != 'Pasted image.png')
{
hasValidAttachments = true;
}
}
if(!hasValidAttachments)
{
g_form.addErrorMessage(message);
return false;
}
}
Server script:
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())
{
attachmentsArray.push(gr.file_name.toString());
}
return JSON.stringify(attachmentsArray);
}
catch(ex) {
var message = ex.message;
return 'ERROR: ' + message;
}
},
type: 'GetAttachments'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2020 10:54 AM
Hi,
This is because the script is asynchronous and it is talking time to get results back from script include and the script in widget is forcing and creating a request.
Try this:
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 {
// START: Query 'sys_attachment' table and verify 'Pasted image.png' is not counted as an attachment
var table_sys_id = gel('sysparm_item_guid').value;
var table_name = 'sc_cart_item';
var ga = new GlideAjax('GetAttachments');
ga.addParam('sysparm_name', 'getAttachments');
ga.addParam('sysparm_table_name', table_name);
ga.addParam('sysparm_table_sys_id', table_sys_id);
ga.getXML(function getAttachments(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var attachments = JSON.parse(answer);
var file_name = "";
var hasValidAttachments = false;
for(var i = 0; i < attachments.length; i++) { //Loop into the array
file_name = attachments[i];
if(file_name != 'Pasted image.png')
{
hasValidAttachments = true;
}
}
if(!hasValidAttachments)
{
g_form.addErrorMessage(message);
return false;
}
});
// END: Query 'sys_attachment' table and verify 'Pasted image.png' is not counted as an attachment
}
catch (e) {
// for Service Portal
var count = getSCAttachmentCount();
if(count <= 0) {
g_form.addErrorMessage(message);
return false;
}
}
}
}
Check this out:
https://snprotips.com/blog/2018/10/19/synchronous-lite-onsubmit-catalogclient-scripts
Thanks,
Ashutosh