- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2019 05:14 AM
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,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2019 04:01 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2019 05:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2019 05:34 AM
Hi,
thanks,
Can you share sample script on the same
thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2019 05:34 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2019 02:19 AM
Hi,
Can you please share script for the same
thanks