- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 01:32 AM
Hi,
Assigned to field is list collector data type. when active value changes into false into sys_user table, the value should deactivate into assigned_to field also(cmdb_Ci table). I have tried the below script but it removed all assigned_to field value. pls let me know the changes in the script. Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 01:57 AM
A list field contains the comma separated sys_id's of the users and you are setting the entire field to 'empty', so that's just doing exactly what you explain it does.
Try something like this:
(function executeRule(current, previous /*null when async*/) {
var userSysId = current.sys_id.toString();
var cmdbCiGr = new GlideRecord('cmdb_ci');
cmdbCiGr.addQuery('assigned_to', 'CONTAINS', userSysId);
cmdbCiGr.query();
while (cmdbCiGr.next()) {
var assignedTo = cmdbCiGr.getValue('assigned_to');
var assignedToArray = assignedTo.split(',');
// Remove the inactive user's sys_id from the assigned_to list
var index = assignedToArray.indexOf(userSysId);
if (index > -1) {
assignedToArray.splice(index, 1);
cmdbCiGr.setValue('assigned_to', assignedToArray.join(','));
cmdbCiGr.update();
}
}
})(current, previous);
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 01:58 AM
Hello @AMan1997 ,
No I run business rule as After only, please check screen short again if you don't mind.
Thank you
G Ramana Murthy
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 03:44 AM
The field is of list type as is in the question. This will do exactly the same as the BR already in the question
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 01:57 AM
A list field contains the comma separated sys_id's of the users and you are setting the entire field to 'empty', so that's just doing exactly what you explain it does.
Try something like this:
(function executeRule(current, previous /*null when async*/) {
var userSysId = current.sys_id.toString();
var cmdbCiGr = new GlideRecord('cmdb_ci');
cmdbCiGr.addQuery('assigned_to', 'CONTAINS', userSysId);
cmdbCiGr.query();
while (cmdbCiGr.next()) {
var assignedTo = cmdbCiGr.getValue('assigned_to');
var assignedToArray = assignedTo.split(',');
// Remove the inactive user's sys_id from the assigned_to list
var index = assignedToArray.indexOf(userSysId);
if (index > -1) {
assignedToArray.splice(index, 1);
cmdbCiGr.setValue('assigned_to', assignedToArray.join(','));
cmdbCiGr.update();
}
}
})(current, previous);
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 08:12 AM
Thanks for this post. this script examines and remove only one row only. i need to remove a single user (Deactivating username) from the list of all those cmdb_ci list.
How could i execute this code when we have multiple rows contains same username ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 12:18 AM
The script does this for all CI's: the while condition takes care of that.
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark