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

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Vijay Baokar ,

 

Use before BR not after BR

 

(function executeRule(current, previous ) {

    if (current.active == false && previous.active == true) {

        var userId = current.sys_id.toString(); 
        var managerId = current.manager.toString(); 

        var chg = new GlideRecord("change_request");
        chg.addQuery("active", true); // Active changes
        chg.addQuery("requested_by", userId);
        chg.query();

        while (chg.next()) {
            var watchList = chg.watch_list.toString();
            var newWatchList = watchList.replace(userId, managerId);
            if (watchList != newWatchList) {
                chg.watch_list = newWatchList;
            }
  
            chg.requested_by = managerId;
            chg.update();
        }
    }

})(current, previous);

 

 


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

LinkedIn - Lets Connect

Hi @Sohail Khilji Thanks for the quick response. If i have do the same for other tables as well like group, Incident, Problem etc shall i do multiple glide records in the same BR?

Yes u can


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

LinkedIn - Lets Connect

@Sohail Khilji Any reason why before BR?