send notification to users who have not logged in for last 90 days

umaaggarwal
Giga Guru
Giga Guru

Hi All,

 

I want to Find out all the users in SN who has not logged in last 3 months and send out an email to them asking to login

Thanks!

 

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi Uma,

Create an event.

Create a notification which listens to this event.

Then write a server-side script, something like below

var gr = new GlideRecord('sys_user');
gr.addNotNullQuery('last_login');
gr.query();
var gdt = new GlideDateTime();
gdt.addDaysUTC(-90);
while(gr.next())
{
  if(gr.getValue('last_login') < gdt.getLocalDate())
  {
    gs.eventQueue('notify.to.login', gr, gr.user_name, gr.email);
  }
}

View solution in original post

4 REPLIES 4

djohnson1
Mega Guru

umaaggarwal, 

 

     Create a notification on the sys_user table with the When to Send condition set to Last login at or before last 3 Months. In the What it will contain section you can provide the link to your instance with this line: Browse to https://" + gs.getProperty("instance_name") + ".service-now.com\n").

 

Thanks, 

Derrick Johnson

asifnoor
Kilo Patron

Hi Uma,

Create an event.

Create a notification which listens to this event.

Then write a server-side script, something like below

var gr = new GlideRecord('sys_user');
gr.addNotNullQuery('last_login');
gr.query();
var gdt = new GlideDateTime();
gdt.addDaysUTC(-90);
while(gr.next())
{
  if(gr.getValue('last_login') < gdt.getLocalDate())
  {
    gs.eventQueue('notify.to.login', gr, gr.user_name, gr.email);
  }
}

Ruhi Jibhyenka1
Mega Guru

Hi Uma,

Go through following steps

1. Go to events and create a new Event, and name it say user.loggedin

2. Create a notification, which can consume that event as Email Notifications - ServiceNow Wiki

3. Write code to create events in the system for all the targeted users.

4. You will have to set this up as a Nightly Schedule.

Please make sure check 

Event param1 contains recipients in the 'who will receive' tab

sendNotifications();

function sendNotifications(){

//user.loggedin

var gr = new GlideRecord("sys_user");

gr.addEncodedQuery('last_login_time<javascript:gs.daysAgoStart(90)');

gr.query();

while(gr.next()){

  gs.eventQueue('user.loggedin', gr, gr.email, gr.user_name)

}

OR

Write a scheduled job

Make sure your conditions you want to apply before executing this, because this involves deleteMultiple();

Script:

var gr= new GlideRecord('sys_user');

gr.addEncodedQuery('active=true^last_loginRELATIVELT@dayofweek@ago@90'); // this gets all the users who are active and last logged in 3 months(90) ago

gr.query();

while(gr.next()){

gr.deleteMultiple();

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy.

Thanks,

Ruhi.

 

ChanakyaMekala
Kilo Guru

Hi Uma

 

We do have a field called Last Login Time on users table which would help you here.

1. Create an event

2. Create a notification which would be sent if the event created in step 1 is triggered.

3. Create a property like Login deadline(login_deadline). This property is not necessarily needed but it is the best practice if you would like to changed the days at any point of time in future.

4. Create a Scheduled Job which would run every day having a script like below:

var loginTime = gs.getProperty('login_deadline');
var time= parseInt(loginTime );
var queryTime = new GlideDateTime();
queryTime.addDaysUTC(-time);

if (time> 0) {
var gr = new GlideRecord('sys_user');
gr.addQuery('last_login_time', '<', queryTime);
gr.query();

}


while(gr.next()) {

gs.eventQueue('<event created in step 1>',gr,'','');

}

 

Please mark Correct or Helpful if it had so