We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Need to change Manager of almost 50 users

VyankteshK
Tera Contributor

Hello Team,

 

I need to change the manager of Almost 50 users , How Can I achieve this by using Background script

 

Thanks In advance. 

7 REPLIES 7

SP22
Giga Sage

Hi @VyankteshK 

 

// Construct the encoded query string
var encodedQuery = 'department=Your Department^active=true';

// Query for users that need to have their manager updated
var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery(encodedQuery);
userGr.query();

// Set the new manager's user ID
var newManagerID = 'new_manager_user_id';

// Loop through the users and update their manager
while (userGr.next()) {
    userGr.manager = newManagerID;
    userGr.update();
    gs.info('Manager updated for user ' + userGr.name);
}

 


Please find the above sample script and do changes as per your requirement.

Thanks
SP.  

This is good, just to add I would add set limit to 50

Also you can use the below , if it fits your use case

 

gr.autoSysFields(false); //do not update system fields
gr.setWorkflow(false); //do not run business rules
-Anurag

Sohail Khilji
Kilo Patron

50 is a small number you can simply do it visa list view change. As suggested by @Dr Atul G- LNG 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect