- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi,
I have an issue where when a user logs in I want the system to change the manager to a set person if the manager field is either empty or the manager in there is either inactive or locked out.
I have an async business rule set to run after update and run the below script but it doesn't seem to be triggering or working as expected.
(function executeRule(current, previous /*null when async*/) {
var currentUserID = currentUser.getID();
var user = new GlideRecord('sys_user');
if (user.get(currentUserID)) {
var managerID = user.manager;
var manager = new GlideRecord('sys_user');
// Check if manager is empty
if (!managerID) {
jslog('No manager assigned. Changing manager for user: ' + user.user_name);
user.manager = '7c7b45b31bfa7910bc98eb59b04bcb76'; // Replace with the new manager's sys_id
user.update();
return;
}
// Check if the current manager is active or not locked out
if (manager.get(managerID)) {
if (!manager.active || manager.locked_out) {
jslog('Manager is inactive or locked out. Changing manager for user: ' + user.user_name);
user.manager = '7c7b45b31bfa7910bc98eb59b04bcb76'; // Replace with the new manager's sys_id
user.update();
}
}
}
function jslog(message) {
gs.info('Business Rule: Update Manager on Login - ' + message);
}
})(current, previous);
Any help to get this working would be greatly appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi @wiganmichae ,
I tried your code in my PDI and it is working fine for me please check below code
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var currentUserID = current.sys_id; // I made changes here
gs.log("currentUserID = " + currentUserID);
var user = new GlideRecord('sys_user');
if (user.get(currentUserID)) {
var managerID = user.manager;
gs.log("Manager çheck = " + managerID);
var manager = new GlideRecord('sys_user');
// Check if manager is emptyi
if (!managerID) {
gs.log('No manager assigned. Changing manager for user: ' + user.user_name);
user.manager = '4ddc2f1193576a109305f520ed03d681'; // Replace with the new manager's sys_id
user.update()
}
// Check if the current manager is active or not locked out
if (manager.get(managerID)) {
if (!manager.active || manager.locked_out) {
gs.log('Manager is inactive or locked out. Changing manager for user: ' + user.user_name);
user.manager = '4ddc2f1193576a109305f520ed03d681'; // Replace with the new manager's sys_id
user.update()
}
}
}
})(current, previous);
Result
Please mark my answer correct and helpful if this works for you
Thanks and Regards,
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
This is working fine now, thanks for the help everyone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
that's not how manager are populated in sys_user.
You don't have AD/LDAP to bring in users.
also you want the manager to be updated when user logins so will that business rule run when user logs in?
try this and it will work if BR condition satisfies
Make BR as Before Update and Below Condition
Last Login time [Changes]
(function execute(inputs, outputs) {
var userId = gs.getUserID();
if (!userId)
return;
var user = new GlideRecord('sys_user');
if (!user.get(userId))
return;
var managerId = user.manager.sys_id; // or user.getValue('manager')
var updateNeeded = false;
var newManagerSysId = '7c7b45b31bfa7910bc98eb59b04bcb76'; // target manager sys_id
if (!managerId) {
updateNeeded = true;
gs.info('No manager assigned. Updating manager for user: ' + user.user_name);
} else {
var manager = new GlideRecord('sys_user');
if (manager.get(managerId)) {
if (!manager.active || manager.locked_out) {
updateNeeded = true;
gs.info('Manager inactive/locked. Updating manager for user: ' + user.user_name);
}
}
}
if (updateNeeded) {
current.manager = newManagerSysId;
}
})(inputs, outputs);
💡 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
3 weeks ago
Hi,
Yes I understand that but we have an issue with how Okta is doing some users and this is a fix for that until we can convince the cyber team to fix the issue. Basically trying to fix an issue that affects a small number of users who don't get assigned correctly or at all.
