Need to check if user is not active in last 90 days client script on a catalog item

kamal11
Giga Expert

Hi All,

 

We do have a variable on the catalog item called "u_user" which refers to user table,

 

We need to validate if the selected user is not active from last 90 days and populate an alert based on the saem

 

can you let us know how to achieve the same

 

thanks,

1 ACCEPTED SOLUTION

Subhadip Saman1
Mega Expert

Hello,

 

You could use the below onChange client script for your requirement:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below

var usr = g_form.getReference('u_user');

var currentTime = new Date().getTime();
var loginTime = new Date(usr.last_login_time.toString()).getTime();

var diff = (currentTime - loginTime)/(24*60*60*1000);

if(diff < 90)
alert('The user logged in within past 90 days.');
else
alert('The user didn\'t logged in for the last 90 days.');

}

 

But it's not a best practice to use getReference in your code, so you could use GlideAjax and a script include to achieve the same.

 

Script Include : 

Name = CheckUserLogin

var CheckUserLogin = Class.create();
CheckUserLogin.prototype = Object.extendsObject(AbstractAjaxProcessor, {
userLogin : function() {
var usr = this.getParameter('sysparm_user_name');
var gr = new GlideRecord('sys_user');
gr.get(usr);

var now = new GlideDateTime(gs.daysAgo(90));
var usrTime = new GlideDateTime(gr.last_login_time);
return usrTime.onOrAfter(now);
},
type: 'CheckUserLogin'
});

 

onChange Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below

var ga = new GlideAjax('CheckUserLogin');
ga.addParam('sysparm_name', 'userLogin');
ga.addParam('sysparm_user_name', g_form.getValue('u_user'));
ga.getXML(callback);
}

function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true')
alert('The user logged in within past 90 days.');
else
alert('The user didn\'t logged in for the last 90 days.');
}

----------------------------------------------------------------------------------------------

Best Regards,

Subhadip

 

 

View solution in original post

7 REPLIES 7

Here is an example of the full script I used to remove user from groups after 45 days of inactivity.  You should be able to modify it slightly to fit your needs.  This was in a scheduled job.  I also kept which group that got removed from in a table so I could easily restore them to their groups if needed.

var uGroup = new GlideRecord ('sys_user_grmember');
uGroup.addEncodedQuery("user.last_login_timeRELATIVELT@dayofweek@ago@45");
uGroup.query();
var LastLogin = new GlideRecord ('u_group_role_removal_automation_audit');
LastLogin.initialize();
var g = 0;
while (uGroup.next()){
	LastLogin.u_name = uGroup.user;
	LastLogin.u_last_login = uGroup.user.last_login_time;
	LastLogin.u_removed_from = uGroup.group.name;
	LastLogin.u_reason = 'Last Login Over 45 Days';
	LastLogin.insert();
	uGroup.deleteRecord();
	g++;
}

Subhadip Saman1
Mega Expert

Hello,

 

You could use the below onChange client script for your requirement:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below

var usr = g_form.getReference('u_user');

var currentTime = new Date().getTime();
var loginTime = new Date(usr.last_login_time.toString()).getTime();

var diff = (currentTime - loginTime)/(24*60*60*1000);

if(diff < 90)
alert('The user logged in within past 90 days.');
else
alert('The user didn\'t logged in for the last 90 days.');

}

 

But it's not a best practice to use getReference in your code, so you could use GlideAjax and a script include to achieve the same.

 

Script Include : 

Name = CheckUserLogin

var CheckUserLogin = Class.create();
CheckUserLogin.prototype = Object.extendsObject(AbstractAjaxProcessor, {
userLogin : function() {
var usr = this.getParameter('sysparm_user_name');
var gr = new GlideRecord('sys_user');
gr.get(usr);

var now = new GlideDateTime(gs.daysAgo(90));
var usrTime = new GlideDateTime(gr.last_login_time);
return usrTime.onOrAfter(now);
},
type: 'CheckUserLogin'
});

 

onChange Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below

var ga = new GlideAjax('CheckUserLogin');
ga.addParam('sysparm_name', 'userLogin');
ga.addParam('sysparm_user_name', g_form.getValue('u_user'));
ga.getXML(callback);
}

function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true')
alert('The user logged in within past 90 days.');
else
alert('The user didn\'t logged in for the last 90 days.');
}

----------------------------------------------------------------------------------------------

Best Regards,

Subhadip

 

 

Hi,

IS there a way we can add 2 or 3 conditions

 

like active is false and updated is more than 90 days?

 

thanks,