Scheduled Job - Query users that has the last login 91 days from today

hongsok
Tera Contributor

Hi Friends,

 

I have created the following Scheduled Job to run daily to find out who has not login for 91 days and send them the email notification. It is not working. I would appreciate if you can help me on this.

 

Regards,

Hong

_______________________________________________________

var gr = new GlideRecord('sys_user');
gr.addNotNullQuery('last_login');
//gr.addEncodedQuery('active=false^u_account_request_numberISNOTEMPTY');
gr.query();
var gdt = new GlideDateTime();
gdt.addDaysUTC(-91);
while (gr.next()) {
    if (gr.getValue('last_login') == gdt.getLocalDate()) {
        gs.eventQueue('user.account.deactivated', gr, '');
    }
}

_______________________________________________

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Run something like this as a Fix Script to see the results vs what is expected, then adjust the numbers if needed before disabling the print line and uncommenting the eventQueue for your scheduled script.  In a PDI or non-prod instance you should be able to update Last login in a list view to test the results.

var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('last_login!=NULL^last_loginRELATIVELT@dayofweek@ago@89^ last_loginRELATIVEGT@dayofweek@ago@91');
userGr.query();
while (userGr.next()) {
	gs.print(userGr.name); //temporary for troubleshooting
    //gs.eventQueue('user.account.deactivated', gr, '');
}

View solution in original post

5 REPLIES 5

Bert_c1
Kilo Patron

 

var usr = new GlideRecord('sys_user');
usr.addQuery('last_login', '<', gs.daysAgo(90));
usr.query();
while (usr.next()) {
	gs.info('User ' + usr.name + ' last_login ' + usr.last_login);
}

 

works in my instance. Add another 'addQuery(); if you like. Queue the event in the 'while' loop.

hongsok
Tera Contributor

Hi,

If we user "usr.addQuery('last_login', '<', gs.daysAgo(90));" the notification will keep send to the user start from 91. We would like to inform the user only once.

You make the user in-active, then add 

 

 

 

var usr = new GlideRecord('sys_user');
usr.addQuery('last_login', '<', gs.daysAgo(90));
usr.addQuery('active', true);			// only look for active users
usr.query();
while (usr.next()) {
	gs.info('User ' + usr.name + ' last_login ' + usr.last_login);
	usr.active = false;
	usr.update();
}

 

 

Or just have your scheduled job run weekly, before making the user account in-active.

Brad Bowman
Kilo Patron
Kilo Patron

Run something like this as a Fix Script to see the results vs what is expected, then adjust the numbers if needed before disabling the print line and uncommenting the eventQueue for your scheduled script.  In a PDI or non-prod instance you should be able to update Last login in a list view to test the results.

var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('last_login!=NULL^last_loginRELATIVELT@dayofweek@ago@89^ last_loginRELATIVEGT@dayofweek@ago@91');
userGr.query();
while (userGr.next()) {
	gs.print(userGr.name); //temporary for troubleshooting
    //gs.eventQueue('user.account.deactivated', gr, '');
}