Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Fix Script for Replacing Manager

nyrguy451
Tera Contributor

Hello All,

 

Let me preface by stating I am not a developer but an admin and have a requirement to create a fix script that will run on demand to update managers when one leaves the organization with another user within the sys_user table.  We have multiple companies within our instance so it would need to query Company, Manager, and replace with another active sys_user.  This would be updated as a per-case instance.

 

basically something like this:  

Query sys_user table for "Company" and "John Doe" as manager, and replace "John Doe" with "Jane Doe" for all users that have "John Doe" as the manager additionally within the script, I would need to find pending approvals with the old manager and change to the new manager.

 

Any help on this would be highly appreciated.

 

Best,

1 ACCEPTED SOLUTION

And here's my version, that adds Company, and approvals state in "Not yet requested" and "Requested"

 

 

// Replace Manager
// 1. re-assign users to new manager
// 2. re-assign "Aprovals" to new manager
var oldManager = 'c092a629db66ae00580ed211ce9619a0';		// sys_id of sys_user record
var newManager = 'ee826bf03710200044e0bfc8bcbe5de6';		// sys_id of new manager sys_user record
var managerCompany = '31bea3d53790200044e0bfc8bcbe5dec';	// sys_id of the company record
var dryRun = true;

// Re-assign user manager
var userRecord = new GlideRecord('sys_user');
var userQuery = 'manager='+oldManager + '^company=' + managerCompany;
userRecord.addEncodedQuery(userQuery);
userRecord.query();
gs.info("ReplaceManagerScript: Found " + userRecord.getRowCount() + " user records to be re-assigned.");
while (userRecord.next()) {
	userRecord.manager = newManager;
	gs.info("ReplaceManagerScript: updating manager for user: " + userRecord.user_name);
	if (!dryRun)
		userRecord.update();
}

// Re-assign approvals
var appRecord = new GlideRecord('sysapproval_approver');
// Only update those that will need approval
var appQuery = 'stateINnot requested,requested^approver=' + oldManager;
appRecord.addEncodedQuery(appQuery);
appRecord.query();
gs.info("ReplaceManagerScript: Found " + appRecord.getRowCount() + " approval records to be re-assigned.");
while (appRecord.next()) {
	appRecord.approver = newManager;
	gs.info("ReplaceManagerScript: updating approval for " + appRecord.document_id);
	if (!dryRun)
		appRecord.update();
}

 

 

Set dryRun to false after testing. And of course, set the sys_id values for the new and old manager, and company.

View solution in original post

5 REPLIES 5

Thank you, everyone, I really appreciate your input and answers.