- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 01:57 PM
Hello,
I have a client script that calls Script Includes to populate a "Reference" type variable. This is working fine using it in an Alert, but I'm having trouble getting the code to work in reference variable.
Could someone please review the code and suggest any fixes? Thank you!
CLIENT SCRIPT:
function onCondition() {
var requestor = g_form.getValue('who_is_this_request_for');
var ga = new GlideAjax('getUnitManager');
ga.addParam('sysparm_name', 'managerName');
ga.addParam('sysparm_requestor', requestor);
ga.getXMLAnswer(getResponse);
function getResponse(response) {
g_form.setValue('current_unit_manager', response); // current_unit_manager is a reference type variable
}
SCRIPT INCLUDES:
var getUnitManager = Class.create();
getUnitManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
managerName: function() {
var manager = '';
var requestedFor = this.getParameter('sysparm_requestor');
var gr = new GlideRecord('sys_user');
gr.get(requestedFor); // Ensure the 'requestedFor' parameter is valid
if (gr.isValidRecord()) {
manager = gr.manager;
if (manager) {
return this.getManagerTitle(manager); // Call the function to get the manager's title
}
}
return 'No manager found for user'; // More informative message if no manager is found
},
getManagerTitle: function(mgr) {
var usr = new GlideRecord('sys_user');
usr.get('sys_id', mgr);
if (usr.isValidRecord()) {
// Check if both "Group" or "Unit" are in the title
var isGroupAndUnit = usr.title && usr.title.indexOf("Group") != -1 || usr.title.indexOf("Unit") != -1;
if (isGroupAndUnit) {
// If both "Group" or "Unit" are in the title, return the name of the manager
return usr.name;
} else {
// If the manager's title does not contain both "Group" or "Unit", return the next level's manager
if (usr.manager) {
return usr.manager.getDisplayValue(); // Recursive call to find the next manager
}
return 'No higher-level manager found'; // Return this if no manager exists at a higher level
}
}
return 'No manager found'; // Return if no valid user record is found
},
type: 'getUnitManager'
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 02:35 PM
You need to set the sys_id of your manager when setting a reference field, not the name/title.
eg:
managerSysId: function() {
var manager = '';
var requestedFor = this.getParameter('sysparm_requestor');
var gr = new GlideRecord('sys_user');
if(gr.get(requestedFor) && gr.isValidRecord()) { // Ensure the 'requestedFor' parameter is valid
manager = gr.manager;
if (manager) {
return manager.sys_id.toString();
}
}
},
Then in your client script, call this new method and check if it returns a sys_id or not:
function onCondition() {
var requestor = g_form.getValue('who_is_this_request_for');
var ga = new GlideAjax('getUnitManager');
ga.addParam('sysparm_name', 'managerSysId');
ga.addParam('sysparm_requestor', requestor);
ga.getXMLAnswer(getResponse);
function getResponse(response) {
if(response) {
g_form.setValue('current_unit_manager', response); // current_unit_manager is a reference type variable
}
}
}
Of course, you can also return an object from your script to include things like the manager name and a server-side error message along with the sys_id so that you can use the second argument to g_form.setValue() and show an error message from the server-side if you wish.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 08:21 PM
Here’s the use case to provide better context: This is a Cellular Phone Service Request. For the company to issue a mobile device for business use, the request must be approved by the user’s manager, who must have the title "Group Manager".
If the user does not have a "Group Manager," the request should escalate to the next-level manager, who may have a different title, for approval.
The code works correctly when the manager’s title contains "Group", but it fails to find the next-level manager when the user does not have a "Group Manager".
Currently, the code includes three return statements (as noted in the comments). I’ve reviewed the code multiple times, but I’m unable to identify where the mistake is causing the failure.
When you have a moment, could you please review the code? Below is the complete code. Thank you so much for your continued assistance.
var getUnitManager = Class.create();
getUnitManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
managerSysId: function() {
var manager = '';
var requestedFor = this.getParameter('sysparm_requestor');
var gr = new GlideRecord('sys_user');
gr.get(requestedFor);
if (gr.isValidRecord()) {
manager = gr.manager;
if (manager) {
//**** RETURN 1 ******
return this.getManagerTitle(manager.sys_id.toString());
}
}
return 'No manager found for user';
},
getManagerTitle: function(mgr) {
var usr = new GlideRecord('sys_user');
usr.get('sys_id', mgr);
if (usr.isValidRecord()) {
var isGroup = usr.title && usr.title.indexOf("Unit") != -1;
if (isGroup) {
//**** RETURN 2 ******
return usr.sys_id.toString();
} else {
if (usr.manager) {
// Recursively call getManagerTitle to find the next-level manager
//**** RETURN 3 ******
return this.getManagerTitle(usr.manager.sys_id);
}
return 'No higher-level manager found';
}
return 'No manager found';
type: 'getUnitManager'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 08:27 PM
Correct this line of code: it should be looking for "Group" instead of "Unit."
var isGroup = usr.title && usr.title.indexOf("Group") != -1;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 08:43 PM - edited 03-04-2025 08:44 PM
Nothing immediately stands out. One thing though (which might just be a copy-paste mistake), is that you don't have a closing `}` for your `getManagerTitle` function before the `type: 'getUnitManager'`.
Perhaps try and do some debugging on the `getManagerTitle()` by calling it in a background script and passing in your sys_id to test with.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 08:46 PM
Perhaps you can try and ensure the sys_id is passed as a string, but don't think that really matters as the `get()` that's used on it should coerce it to a string automatically:
this.getManagerTitle(usr.manager.sys_id.toString());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 10:11 PM
Hello @Nick Parsons
Thank you again for your assistance. Although the code still hasn’t returned the expected value, I truly appreciate the time and effort you have put into guiding me in the right direction. I will keep working on it after all, that is the best way to learn!