I am trying to implement a scheduled job to deactivate users (active false) whose logged in time is 60 days before and revoke all the roles and groups.

Divya44
Tera Contributor

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

1 ACCEPTED SOLUTION

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

LinkedIn

View solution in original post

5 REPLIES 5

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

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();
}
}

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

LinkedIn