- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 05:28 AM
Hello everyone,
I have this catalog client script that works correctly except that it does not cancel the submission of the form if the condition is met, any ideas.
Greetings
function onChange(control, oldValue, newValue, isLoading) {
// If the field is loading, do nothing.
if (isLoading) {
return;
}
var requestedFor = g_form.getValue('for_whom_are_you_requesting_this_item');
var ga = new GlideAjax('ALOCatalogItemOtherSoftwareAJAX');
ga.addParam('sysparm_name', 'isManager');
ga.addParam('sysparm_user', requestedFor);
ga.getXML(checkManager);
}
function checkManager(response) {
var isManager = response.responseXML.documentElement.getAttribute("answer");
// If the user is a manager, display an error message and cancel the form submission.
if (isManager == 'true') {
g_form.showFieldMsg('for_whom_are_you_requesting_this_item', 'The user is not allowed.', 'error');
// Cancel form submission
g_form.cancelSubmit();
}
// If the user is not a manager, clear the error message.
else {
g_form.clearFieldMsg('for_whom_are_you_requesting_this_item');
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 05:47 AM - edited 02-22-2024 06:01 AM
Hi @Tomas Linde,
The getXML function will not wait for the AJAX to run. You therefore need to stop submitting the form after the getXML command and handle the submission inside the checkManager function (AJAX code). Please try the below code with a few tweaks:
function onChange(control, oldValue, newValue, isLoading) {
// If the field is loading, do nothing.
if (isLoading) {
return;
}
var requestedFor = g_form.getValue('for_whom_are_you_requesting_this_item');
var ga = new GlideAjax('ALOCatalogItemOtherSoftwareAJAX');
ga.addParam('sysparm_name', 'isManager');
ga.addParam('sysparm_user', requestedFor);
ga.getXML(checkManager);
return false;
}
function checkManager(response) {
var isManager = response.responseXML.documentElement.getAttribute("answer");
// If the user is a manager, display an error message and cancel the form submission.
if (isManager == 'true') {
g_form.showFieldMsg('for_whom_are_you_requesting_this_item', 'The user is not allowed.', 'error');
// Cancel form submission
return false;
}
// If the user is not a manager, clear the error message.
else {
g_form.clearFieldMsg('for_whom_are_you_requesting_this_item');
//if there is no other condition to check other than the manager field - control submission here. You can also control this with an if statement if required
g_form.submit();
}
}
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.
Thanks, Robbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 09:52 AM
Try taking the "return false" right after your getXML out. If that doesn't work - open the console log - many times that will show you exactly where the error lies (for example, I've see that a variable being used is coming back undefined and it's causing the error).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 08:38 AM
Of course
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 08:44 AM
And what does the script step look like right now?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 09:19 AM
The false return is common and is commented because it does not work
function onChange(control, oldValue, newValue, isLoading) {
// If the field is loading, do nothing.
if (isLoading) {
return;
}
var requestedFor = g_form.getValue('for_whom_are_you_requesting_this_item');
var ga = new GlideAjax('ALOCatalogItemOtherSoftwareAJAX');
ga.addParam('sysparm_name', 'isManager');
ga.addParam('sysparm_user', requestedFor);
ga.getXML(checkManager);
return false;
}
function checkManager(response) {
var isManager = response.responseXML.documentElement.getAttribute("answer");
// If the user is a manager, display an error message and cancel the form submission.
if (isManager == 'true') {
g_form.showFieldMsg('for_whom_are_you_requesting_this_item', 'The user is not allowed.', 'error');
//return false
}
// If the user is not a manager, clear the error message.
else {
g_form.clearFieldMsg('for_whom_are_you_requesting_this_item');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 09:52 AM
Try taking the "return false" right after your getXML out. If that doesn't work - open the console log - many times that will show you exactly where the error lies (for example, I've see that a variable being used is coming back undefined and it's causing the error).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 05:42 AM