Script to replace inactive user and watchlist.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2024 03:39 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2024 03:42 AM
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....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2024 03:50 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2024 03:51 AM
Yes u can
☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2024 04:01 AM
@Sohail Khilji Any reason why before BR?