glide record not working in onsubmit client script.

uma7
Kilo Guru

find_real_file.pngThis code is giving the alert of if condition and stopping the submission even i am attaching a file.

whats wrong in my code?

1 ACCEPTED SOLUTION

Pooja Mallikarj
Kilo Sage

Hi,

It is not recommended to use GlideRecord in client scripts still if you want to achieve by client scripts then you can try with below code.

function onSubmit() {
//Type appropriate comment here, and begin script below

var cat_id = g_form.getValue('sysparm_item_guid');
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.next()) {
alert("You must add an attachment before submitting this request.");
return false;
}
}

 

Thanks,

Pooja M

View solution in original post

11 REPLIES 11

Gaurav Shirsat
Mega Sage
Hi Uma
 
If You are using the Glide Record in Client Script
 
Gliderecord should not be used in client side scripting and can be easily replaced/remodeled using a script include + Glideajax.

refer my code:-

As mentioned earlier "Glide Record" usage in client script is not a good practice.

You should easily be able to convert your code into a GlideAjax call and/or getReference(callback) function, depending on the need of the requirement. 

var ga = new GlideAjax('script_include_name');
ga.addParam('sysparm_name', 'function_name');
ga.addParam('sysparm_user', g_form.getValue('u_requestor'));
ga.getXML(mycallback);

function mycallback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'yes') { //assuming you are returning yes as answer from script include

//Do logic here for the form, based on the response

}

}

// Script Include

function_name: function() {
var answer ='';
var user = this.getParameter('sysparm_user');
var gr = new GlideRecord('cmdb_ci_computer');

gr.addQuery("assigned_to", user);

gr.addQuery("u_type", "!=", 'monitors');

gr.query();

while(gr.next()){

//do the logic here and set the answer variable to a value which you will use in the client slide. 
answer = //Do the logic here...
}

return answer;
}

 

Please Mark Correct and Helpful

Thanks and Regards

Gaurav Shirsat

 

Manoj Kumar16
Giga Guru

why don't you use OOB Mandatory Attachment checkbox to achieve this requirement?

find_real_file.png 

Manoj Kumar16
Giga Guru

And the g_form.getUniqueValue() will give you the sys_id which is present in the URL of the current form which will not match with the table_sys_id which you have in the sys_attachment table. Try to put an alert for the variable 'gr' you are using in the script and compare that with the table_sys_id of the attachment in the sys_attachment table. 

It will not match.

Pooja Mallikarj
Kilo Sage

Hi,

It is not recommended to use GlideRecord in client scripts still if you want to achieve by client scripts then you can try with below code.

function onSubmit() {
//Type appropriate comment here, and begin script below

var cat_id = g_form.getValue('sysparm_item_guid');
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.next()) {
alert("You must add an attachment before submitting this request.");
return false;
}
}

 

Thanks,

Pooja M