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

Hi Sanjiv, 

sorry I got diverted by another priority, shooting to try your script this week...I'll get back with you...thanks again!

could this be a Jakarta bug?

Hi Sanjiv, picking this ball back up.  But this time using it in a BEFORE business rule:  here's my script, still not working.  

Here's my script...it's not reaching line 6.  Below is the error in the log

 

find_real_file.png

find_real_file.png

Hi Sanjiv and Michael,

so in the context of a Before business rule, this proved to be quite easy since we have access to "current".  In this capacity I needed to prevent setting the state on a Problem record if there was no attachment, and this script below worked perfectly.  Still going to need be able to do the same thing on catalog items in Service Portal, so still working on that.  thanks again you guys for your help as always!

(function executeRule(current, previous /*null when async*/) {

// Check for attachment
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_name','problem');
attachment.addQuery('table_sys_id',current.sys_id);
attachment.query();

if (!attachment.next()){
gs.addErrorMessage('Please attach the completed RCA Template before setting the State to Pending Review');
current.setAbortAction(true);
}
})(current, previous);

Michael Fry1
Kilo Patron

This thread has a solution that works in both desktop view and service portal: https://community.servicenow.com/community?id=community_question&sys_id=dd618729db98dbc01dcaf3231f96...