Script to replace inactive user and watchlist.

Vijay Baokar
Kilo Sage

Hi All,

I am trying to write a business rule on user table, suppose if the user becomes inactive then reassign all his change requests which he requested to his manager and if the inactive user is in the watchlist as well then replace him with his manager as well with out overwrite the entire list field.

After (Update)

Active changes to false :

 

var user = current.getValue("sys_id");// Inactive user 

var chg = new GlideRecord("change_request");

  chg.addEncodedQuery("active=true^requested_by=" + user);

 

  chg.query();

  while(chg.next()){

     var manager = chg.requested_by.manager.toString();

    var whatlst = chg.watch_list.toString();

    var newWatchlist = whatlst.replace(user,manager);

    chg.watch_list = newWatchlist;

      chg.requested_by = manager;

   

    chg.update();

  }

 

Is this correct?

5 REPLIES 5

Community Alums
Not applicable

Hi @Vijay Baokar ,

I tried your problem in my PDI and it works for me please refer below code 

Create before BR 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    gs.log("Here Check active user");

	var assignedTo = current.assigned_to;
	gs.log("assignedTo = " + assignedTo);
	var gr = new GlideRecord('sys_user');
	gr.addQuery('sys_id', assignedTo);
	gr.query();
	if(gr.next()){
			gs.log("Chekc active status = " + gr.active);
		if(gr.active == false ||  gr.active == 'false'){
			current.setValue('assigned_to', gr.manager);
		}
	}

})(current, previous);

SarthakKashyap_0-1715080086230.png

 

Result 

SarthakKashyap_1-1715080144223.png

I assigned change request to any user which is active before, now I'm deactivating that user and now change request assigned to his manager 

SarthakKashyap_2-1715080228039.png

Result in change request 

SarthakKashyap_4-1715080291981.png

 

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak