- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2022 12:36 AM
I tried below script in Scheduled job to make active false for the users and revoke roles and groups.Its removing roles and groups but the active field is still true. I want active field to set to false based on the condition.
Kindly Share your views.
var license = new GlideRecord("sys_user");
license.addQuery("last_login_time<=javascript:gs.beginningOfLast60Days()^active=true^u_user_type!=1");
license.orderByDesc('last_login_time');
license.setLimit(5);
license.query();
license.active ='false';
license.update();
while(license.next()){
gs.log("users list first five id's" +license.user_name);
var gr= new GlideRecord('sys_user_grmember');
gr.addQuery("user",license.sys_id);
gr.query();
if(gr.next()){
gr.deleteMultiple();
}
var gr1= new GlideRecord('sys_user_has_role');
gr1.addQuery("user="+license.sys_id+"^role!=7fcaa702933002009c8579b4f47ffbde^ORrole=NULL");
gr1.query();
if(gr1.next()){
gr1.deleteMultiple();
}
}
Thanks in advance
DM
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2022 03:11 AM
Ah my mistake. The license.update() needs to be after the .next, though also:
license.active ='false';
So something like:
while(license.next()) {
license.active = false;
license.update();
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2022 12:39 AM
Hi there,
What's the reason for using a Scheduled Job? Why not going for zero code, a Scheduled Flow?
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2022 12:41 AM
Anyway, the license.update(); is positioned incorrectly. You have to perform a .next() first. So for example move the license.update() one line down, after the while(license.next()){
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2022 01:44 AM
Thanks for your input Mark!!
I have modified as per your suggestion, but still i am unable to achieve Active false and log is also not printing
var license = new GlideRecord("sys_user");
license.addQuery("last_login_time<=javascript:gs.beginningOfLast60Days()^active=true^u_user_type!=1");
license.orderByDesc('last_login_time');
license.setLimit(5);
license.query();
license.active ='false';
while(license.next()){
license.update();
gs.log("users list first five id's" +license.user_name);
var gr= new GlideRecord('sys_user_grmember');
gr.addQuery("user",license.sys_id);
gr.query();
if(gr.next()){
gr.deleteMultiple();
}
var gr1= new GlideRecord('sys_user_has_role');
gr1.addQuery("user="+license.sys_id+"^role!=7fcaa702933002009c8579b4f47ffbde^ORrole=NULL");
gr1.query();
if(gr1.next()){
gr1.deleteMultiple();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2022 03:11 AM
Ah my mistake. The license.update() needs to be after the .next, though also:
license.active ='false';
So something like:
while(license.next()) {
license.active = false;
license.update();
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field