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

felladin
Tera Guru

Hello,

I believe the field on user table you want to look at is Last Login Time.
Do an onChange script that gets that field and then checks if the time is older than 90 days ago.

https://developer.servicenow.com/app.do#!/api_doc?v=kingston&id=SGSYS-daysAgo_N

With regards
Anton

Hi,

 

thanks,

Can you share sample script on the same

 

thanks

Brian Lancaster
Tera Sage

You can use an encoded query like this to determine their last login.  This is from a script that I had for 45 days.

last_login_timeRELATIVELE@dayofweek@ago@45

Hi,

 

Can you please share script for the same

 

thanks