- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2020 09:11 PM
This code is giving the alert of if condition and stopping the submission even i am attaching a file.
whats wrong in my code?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2020 11:24 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2020 09:18 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2020 09:21 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2020 09:40 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2020 11:24 PM
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