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

Kartik Choudha1
Tera Guru

Hi ,

 

Fix script can be written for this.

As you mentioned you want to update the existing User in sys_user table with the new manager's name. You need to decide first like what all fields you want to update in existing user.

 

You need to update the userID , name, emailID etc.

Generally , if user leaves the organisation, we use to make that particular user record 'inactive'. We have active field present.

 

Regards,

Kartik

Kartik Choudha1
Tera Guru

Pls Find the fix script for this.

You need to update the user_name and company field for different users in encoded query.

//Query existing Manager/user who left the org. Query is based on userID + Company because name can be same for different user that’s why using userID
var grUser = new GlideRecord(‘sys_user’);
grUser.addEncodedQuery(‘company=227cdfb03710200044e0bfc8bcbe5d6b^user_name=jimmie.barninger’);
grUser.query();
while(grUser.next()){
//replace with new manager/user
grUser.first_name = ‘Replace’;
grUser.last_name = ‘user’;
grUser.email = ‘rep@company.com’;
grUser.user_name = ‘rep.cho’;
grUser.update();
}

Existing record has been updated with new details -

KartikChoudha1_0-1679578842227.png

 

Let us know if any understanding required on this,.

Teja11
Giga Guru

Hi @nyrguy451 ,

 

try the below code, this worked for me

 

 

var oldManager = '69a638365b722200a9ad09ed5a62bceb';   //sys_id of old manager user record
var newManager = '122ac7f0d7101200a9addd173e24d411';  // sys_id of new manager user record

var userGr = new GlideRecord('sys_user');
userGr.addQuery('manager', oldManager);
userGr.addQuery('company','223ac7f0d7101200a9addd173e24d4122');    //check the company backend name and sys_id
userGr.query();
while (userGr.next()) {
  userGr.manager = newManager;
  userGr.update();

  // Query for any pending approvals with oldManager and update the approver field to newManager
  var appGr = new GlideRecord('sysapproval_approver');
  appGr.addQuery('state', 'requested');
  appGr.addQuery('approver', oldManager);
  appGr.query();
  while (appGr.next()) {
    appGr.approver = newManager;
    appGr.update();
  }
}

 

 

 

Regards,

Teja

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

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.