- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2022 02:42 AM
Hi All,
Need to remove a user from specific group if users not logged in 3o days.
Ex : I have group called ( ITSM_Group ) in that group we have multiple users, If any user not logged in more than 30days we need to remove the user from the group.
Please suggest,
Thanks ALL,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2022 11:24 PM
Hi,
You can try this as well for better optimization
var arr = [];
var gr = new GlideRecord("sys_user");
var gdt = new GlideDateTime();
gdt.addDays(-30);// User not logged in 30 days
gr.addQuery("last_login_time","<=",gdt);
gr.addActiveQuery();
gr.query();
while(gr.next()){
arr.push(gr.getUniqueValue());
}
var grp = new GlideRecord("sys_user_grmember");
grp.addQuery("user", "IN", arr.toString());
grp.addQuery("group", "b85d44954a3623120004689b2d5dd60a");
grp.query();
grp.deleteMultiple();
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2022 02:53 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2022 02:53 AM
Hi,
You can create a Flow that runs daily, that looks at the group member last login date, and removes the membership if needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2022 03:05 AM
you can use this script also
var gr = new GlideRecord("sys_user");
var gdt = new GlideDateTime();
gdt.addDays(-30);// User not logged in 30 days
gr.addQuery("last_login_time","<=",gdt);
gr.addActiveQuery();
gr.query();
gs.info("row------"+gr.getRowCount());
while(gr.next()){
var grp=new GlideRecord("sys_user_grmember");
grp.addQuery("user",gr.sys_id);
grp.query();
while(grp.next()){
if(grp.group=='b85d44954a3623120004689b2d5dd60a'){ // Your Group sys_id
gs.info("Group member rowcount---"+grp.user.name); // No of user not logged in 30 days
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2022 11:12 PM
Hi,
I need to delete multiple records but above code has loop and it only delete one record at a time.
Please suggest.
Thanks,