- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2019 11:37 AM
Hi,
We have a recent requirement. From our ServiceNow prod instance, we have to get a list of groups which are active but with no activity. These groups will be planned to removed.
For Example: In incidents which are opened from January 1, 2018 till date, we need the assignment group names which aren't assigned to any of these incidents (if possible, also group names which aren't part of SLA assignment groups history from 2018 start till date).
Please suggest the best way for same.
Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2019 12:23 PM
Hi,
You can run this script in a fix script or background script and it will print out the sys_id and name of inactive groups (groups that haven't been assigned to incidents) since the first of January 2018:
//begin with a GlideRecord query to return all active groups
var gr = new GlideRecord("sys_user_group");
gr.addActiveQuery();
gr.query();
var inactiveGroups = []; //set up array to hold inactive group results
//loop through all active groups
while (gr.next()) {
var inactiveGroup = {}; //setup object to hold sys_id and display value of each inactive group
var groupSysId = gr.getUniqueValue(); //sys_id of current group
var groupName = gr.getDisplayValue(); //display value of current group
//new GlideRecord to test if the group has been assigned to any incident from the 1st of Jan 2018 to today
var inc = new GlideRecord("incident");
inc.addEncodedQuery("sys_created_onBETWEENjavascript:gs.dateGenerate('2018-01-01','00:00:00')@javascript:gs.endOfToday()^assignment_group=" + groupSysId);
inc.query();
//if no results are returned we record the inactive group details
if (!inc.next()) {
inactiveGroup = {
value: groupSysId,
display_value: groupName
};
inactiveGroups.push(inactiveGroup); //push details to array
}
}
gs.print("Inactive Groups: " + JSON.stringify(inactiveGroups)); //print out the results
Let me know if this worked for you.
Brent
P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2019 12:23 PM
Hi,
You can run this script in a fix script or background script and it will print out the sys_id and name of inactive groups (groups that haven't been assigned to incidents) since the first of January 2018:
//begin with a GlideRecord query to return all active groups
var gr = new GlideRecord("sys_user_group");
gr.addActiveQuery();
gr.query();
var inactiveGroups = []; //set up array to hold inactive group results
//loop through all active groups
while (gr.next()) {
var inactiveGroup = {}; //setup object to hold sys_id and display value of each inactive group
var groupSysId = gr.getUniqueValue(); //sys_id of current group
var groupName = gr.getDisplayValue(); //display value of current group
//new GlideRecord to test if the group has been assigned to any incident from the 1st of Jan 2018 to today
var inc = new GlideRecord("incident");
inc.addEncodedQuery("sys_created_onBETWEENjavascript:gs.dateGenerate('2018-01-01','00:00:00')@javascript:gs.endOfToday()^assignment_group=" + groupSysId);
inc.query();
//if no results are returned we record the inactive group details
if (!inc.next()) {
inactiveGroup = {
value: groupSysId,
display_value: groupName
};
inactiveGroups.push(inactiveGroup); //push details to array
}
}
gs.print("Inactive Groups: " + JSON.stringify(inactiveGroups)); //print out the results
Let me know if this worked for you.
Brent
P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2019 12:25 PM
Hi Ya,
Just checking to see if my script answered your question? If so, please mark as correct or provide additional information about what didn't work. I tested this in my London developer instance and it worked perfectly.
Brent
P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2019 04:55 PM
Hi Raagavi,
Just checking in to see if my script worked for you? If so, please mark as correct so other community members can benefit from this information.
Thanks,
Brent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2019 12:08 AM
Hi Brent,
Thank you so much for your reply and solution.
Actually in our instance, I'm yet to try this, as I'm waiting for elevated access. Once I have got it, I'll try the same and will let you know..
Please wait for 3-4 days.
Thank you,
Raagavi K