- 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 05:40 AM
Why not just put user criteria on the item that if manager is empty, the item is not available for you?
But I think you need to move your 'cancelSubmit' out of your function and into your script itself.
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 05:47 AM
I need to access the catalog item but not select a specific user in one of the variables. I don't know if it can be done with User Criteria
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 05:40 AM
As long as that field is required, you can clear the value in the field instead of using cancelSubmit and it will force them to choose a new person before submission.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 05:58 AM
It seems like a smart solution to me and it works, but it also removes elg_form.showFieldMsg