- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2023 11:19 AM
hi all,
I have the following code in a catalog client script, which is designed to check if the 'requested_for' user has a manager. If not then the form submission for the catalog item should be restricted. At the moment this is not working and I've no idea why so any help would be much appreciated.
function onSubmit() {
var req = g_form.getReference('requested_for', managerCheck);
function managerCheck(req) {
var manager = req.manager;
if (manager == '') {
return false;
}
}
}
Thank you!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2023 07:49 AM
For anyone who has a similar problem, the following code managed to solve it:
Thank you @Vishal Birajdar for some of the solution.
Step 1: Client callable Script Include:
Name : UserUtils
//Function within script include
getManager: function (){
var manager;
var user = this.getParameter('sysparm_user');
//Glide record on user table
var grUser = new GlideRecord ('sys_user');
grUser.addQuery('sys_id',user);
grUser.query();
if(grUser.next()){
manager= grUser.getValue('manager');
}
if(manager){
return true;
}else {
return false;
}
}
Step 2: onSubmit Client script
function onSumbit(){
var ga = new GlideAjax('UserUtils');
ga.addParam('sysparm_name', 'getManager');
ga.addParam('sysparm_user', g_form.getValue('requested_for'));
ga.getXMLWait();
var result = ga.getAnswer();
var abort;
return abort = callbackfunction(result);
}
function callbackfunction(answer) {
if (answer != true) {
g_form.addErrorMessage("Requested for does not have manager");
return false;
} else {
return true;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2023 11:42 AM
You might have to move your managerCheck function out of the onSubmit function
function onSubmit() {
//Type appropriate comment here, and begin script below
var req = g_form.getReference('requested_for', managerCheck);
}
function managerCheck(req) {
var manager = req.manager;
if (manager == '') {
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2023 02:13 AM
Hi there,
Thanks for your response. I tried suggested solution but as the submission of the form is asynchronous I think this is why it is not working.
Thank you again.
G
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2023 12:06 PM
Since manager is a sys_id, I would force it to a string in the variable assignment, just to be safe:
var manager = req.manager.toString();
Otherwise looks fine. Try adding an alert outside of the if block to see what value it thinks is there. If you're trying this on Service Portal/Employee Center, that's a different issue - the form is submitted asynchronously, so it doesn't wait for the getReference to return the server value before completing the submission. Here's a work-around using GlideAjax, or you can do the check onChange of requested_for instead - setting/clearing a (hidden) mandatory variable to prevent submission.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2023 02:12 AM
Hi Brad,
Thanks for your response. It's a form to submit a catalog item so is indeed asynchronous as I wrote this in onSumbit. I will have a go with GlideAjax instead.
G