GlideRecord query not working??

patricklatella
Mega Sage

hi all,

I've got an onSubmit catalog client script that is meant to prevent the submission of a catalog item if there is no attachment already attached to the form.  I'm using a script I've used before, in the other cases it works fine.  However in this catalog client script, the script is breaking on the line identified below.  The upper part of the script is checking if a certain field is empty, and if it is empty it then checks if there's an attachment...and if there is no attachment it should then throw the "alert()".   Anyone see why my var attachment = new GlideRecord('sys_attachment'); line is breaking the script?

thanks!

Here's the full script:

 

function onSubmit() {
//check first if Entry #1 fields are entered
g_form.addInfoMessage('item_number1 is '+g_form.getValue('item_number1'));
if (g_form.getValue('item_number1') != ''){
return;
}
else {
//check for attachment
g_form.addInfoMessage('item_number1 is empty');
var sys_id = gel('sysparm_item_guid').value;
g_form.addInfoMessage('hello'); //this message is being displayed
var attachment = new GlideRecord('sys_attachment'); //this line appears to breaking the script
g_form.addInfoMessage('hello2');// this message is NOT being displayed
attachment.addQuery('table_name','x_cur_erp_sm_request');
attachment.addQuery('table_sys_id',sys_id);
attachment.query();

if (!attachment.next()) {
alert ("Please attach a completed Service Delivery Request form before submitting this request if not using the available fields on the form.");
return false;
}
}

15 REPLIES 15

SanjivMeher
Kilo Patron
Kilo Patron

 

Can you try below script? Also gel('sysparm_item_guid').value works for you in Service Portal?

 

function onSubmit() {
//check first if Entry #1 fields are entered
g_form.addInfoMessage('item_number1 is '+g_form.getValue('item_number1'));
if (g_form.getValue('item_number1') != ''){
return;
}
else {
//check for attachment
g_form.addInfoMessage('item_number1 is empty');
var sys_id = gel('sysparm_item_guid').value; 
g_form.addInfoMessage('hello'); //this message is being displayed
var att = new GlideRecord('sys_attachment'); //this line appears to breaking the script
g_form.addInfoMessage('hello2');// this message is NOT being displayed
att.addQuery('table_name','x_cur_erp_sm_request'); 
att.addQuery('table_sys_id',sys_id); 
att.query(); 

if (!att.next()) {
alert ("Please attach a completed Service Delivery Request form before submitting this request if not using the available fields on the form."); 
return false; 
}
}


Please mark this response as correct or helpful if it assisted you with your question.

Hi Sanjiv,

actually now that you mention it, I've gone and tested and no, "gel('sysparm_item_guid').value" doesn't work for me in Service Portal.

Ok, so maybe I need to start fresh, what script would I use to enforce an attachment onSubmit that works in Service Portal?

I did try your script, and actually scaled it down to the script below...but still not working...it's not reaching the "hello2" addInfoMessage.  

 

find_real_file.png

 

find_real_file.png

Are you running it on a scoped app? Can you run this script in background script and see if you get the result. Try running it on Global and scoped App in background script


Please mark this response as correct or helpful if it assisted you with your question.