- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 12:32 AM
Hi Experts,
I am trying to validate some fields using glide ajax but return false is not working alert is working.
Could you please help me with this?
After validation alert is showing but form gets submitted.
function onSubmit() {
var ga = new GlideAjax('getUserdetails');
ga.addParam('sysparm_name', 'getUserInformation');
ga.addParam('sysparm_user_id', g_form.getValue('caller_name'));
ga.getXML(parseUserResponse);
function parseUserResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'Contractor') {
if (g_form.getValue('work_in_china_or_russia') != 'No') {
alert('Please Select all questions without any Error of Contractor form');
return false;
}
}
}
}
Solved! Go to Solution.
- Labels:
-
Service Catalog
-
Service Desk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 01:12 AM
Hi,
then take this approach
I assume your called name is readonly once it gets auto-populated with logged in user and is not changed.
1) create string variable with name as Employment Type (employment_type) and hide it always using UI policy; keep it at the bottom of the form
2) then in default value of that variable use this to store the employment type
javascript:
var typ;
var user_sys_id = gs.getUserID(); // always logged in user
var user = new GlideRecord('sys_user');
user.addQuery('sys_id',user_sys_id);
user.query();
if (user.next()) {
typ = user.u_employment_type;
}
typ;
3) use this in your onSubmit and compare value stored in hidden variable
function onSubmit() {
var answer = g_form.getValue('employment_type');
if (answer == 'Contractor') {
if (g_form.getValue('work_in_china_or_russia') != 'No') {
alert('Please Select all questions without any Error of Contractor form');
return false;
}
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 12:46 AM
HI,
Async glide ajax wont work on onsubmit client script, you need to use sync glide ajax which is not a best practice.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 12:48 AM
You might have to use
getXMLWait() method instead of getXML()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 12:50 AM
GlideAjax does not work with On submit client script because response is awaited from server and till then the form is submitted.
Change your on submit script to on change script, then it will work.
You can use getXMLWait(), but that does not work on portal
Raghav
MVP 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 12:51 AM
Hi,
why not use onChange client script on called_id
If validation fails then clear the value and make it mandatory
Using this user will have to give valid value and then only form can be submitted.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader