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 04:11 AM
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);
Result
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
Result in change request
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak