Autopopulate the Manager field in the catalog item.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited yesterday
Hello,
I need to automatically populate the Approving Manager variable in a ServiceNow catalog item based on the following logic:
- If the Is this request for you? variable is Yes, then the manager of the Requested by user should be auto-populated in the Approving Manager field.
- If the Is this request for you? variable is No, then the manager of the user specified in the On Behalf of variable should be auto-populated in the Approving Manager field.
Could someone guide me on how to implement this?
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
are you comparing correct yes value
also did you try adding alerts and see?
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
hi @Servicenow de11
Create the Script Include:
-
Name:
UserManagerAjax -
Client callable: Check this box (make it
true)var UserManagerAjax = Class.create(); UserManagerAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { getManagerInfo: function() { var userId = this.getParameter('sysparm_user_id'); var grUser = new GlideRecord('sys_user'); // Check if the user ID is valid, the user exists, and has a manager if (userId && grUser.get(userId) && grUser.manager) { var managerId = grUser.getValue('manager'); var managerName = grUser.getDisplayValue('manager'); // Return the manager's ID and Name as a JSON object var result = {}; result.id = managerId; result.name = managerName; return new JSON().encode(result); } // If no user or no manager, return null return null; }, type: 'UserManagerAjax' });
Catalog Client Script: OnChange (for "Is this request for you?")
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
setManager();
}
function setManager() {
var isForYou = g_form.getValue('is_for_you'); // <--- CHECK YOUR VARIABLE NAME
var targetUserID;
// 2. Decide which user field to check
if (isForYou == 'Yes') {
targetUserID = g_form.getValue('requested_for'); // <--- CHECK YOUR VARIABLE NAME
} else {
targetUserID = g_form.getValue('on_behalf_of'); // <--- CHECK YOUR VARIABLE NAME
}
// 3. If the target user field is empty, clear the manager and stop
if (!targetUserID) {
g_form.clearValue('approving_manager'); // <--- CHECK YOUR VARIABLE NAME
return;
}
// 4. Call our Script Include to get the manager
var ga = new GlideAjax('UserManagerAjax');
ga.addParam('sysparm_name', 'getManagerInfo');
ga.addParam('sysparm_user_id', targetUserID);
ga.getXML(handleResponse);
}
}
function handleResponse(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer) {
try {
// Parse the JSON string from the server
var manager = JSON.parse(answer);
// Set the 'approving_manager' field
g_form.setValue('approving_manager', manager.id, manager.name); // <--- CHECK YOUR VARIABLE NAME
} catch (e) {
// Error parsing, clear the field
g_form.clearValue('approving_manager'); // <--- CHECK YOUR VARIABLE NAME
}
} else {
// No manager was found, clear the field
g_form.clearValue('approving_manager'); // <--- CHECK YOUR VARIABLE NAME
}
}
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning
Thanks & Regards
Deepak Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hello Deepak,
I have tried with the above script but it is not auto populating.
