Send notification once at 30 days and 60 days

Bridgina Starr
Tera Contributor

Hi,

I am not sure I am putting this in the right forum but hope someone can assist.

I have created 2 notifications to send to ITIL users that have not logged in for over 30 days and 60 days.

 

Scenario 1:

ITIL user has not logged in for over 30 days

  1. Event: Last.login

  2. Notification: ITIL users not logged in for over 30 day

    1. Send When: Event is fired
  3. Scheduled job: Send ITIL not logged in for 30 days

    1. var gr = new GlideRecord('sys_user');
      gr.addEncodedQuery('roles=ITIL^active=true^last_login<javascript&colon;gs.beginningOfLast30Days()^ORlast_loginISEMPTY');
      gr.query();
      while(gr.next()){
      gs.eventQueue("Last.login", gr);
      }
  4. Job runs daily
  5. Issue: Some users are getting the email notification every day and I only need it to send to them once 

Scenario 2:

ITIL Users not logged in for over 60 days

Created in Dev:

  1. Event: Last.login60

  2. Notification: ITIL users not logged in for over 60 day

    1. Send when: Event is fired
  3. Scheduled job: Send ITIL not logged in for 60 days

    1. var gr = new GlideRecord('sys_user');
      gr.addEncodedQuery('roles=ITIL^active=true^last_login<javascript&colon;gs.beginningOfLast60Days()^ORlast_loginISEMPTY');
      gr.query();
      while(gr.next()){
      gs.eventQueue("Last.login60", gr);
      }
  4. Job runs daily
  5. Issue: Some users are getting the email notification every day and I only need it to send to them once

I would appreciate any assistance on how to have the job run daily but only send the notification to the user once.

 

Thanks,

Gina

6 REPLIES 6

Community Alums
Not applicable

Hello @Bridgina Starr 

 

Can you please try the below code

 

var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('roles=ITIL^active=true^last_login<javascript&colon;gs.daysAgoStart(30)');
gr.query();
while(gr.next()){
gs.eventQueue("Last.login", gr);
}

 

Don't forget to mark my answer as Correct & Helpful, if applicable.

Krishna Sharma

Aylee Andersen
Kilo Sage

Here are a couple different ideas that you could try:

  • Change your encoded query so that it only runs when the last_login is equal to the 30 or 60 days, rather than using "<". This could work if the job is running every day and if a last_login value exists. This wouldn't work for users that have already had their last_login time exceeding 60 days.
  • Add a hidden field to sys_user to keep track of whether or not that user has had a notification already. For example, you could have a date field that stores when they were sent their most recent last login notification. That way, your encoded query could check the last_login date as well as the previous last login notification timestamp to determine whether or not another notification should be sent.

I'm sure there are other solutions out there, but those were two fairly simple ideas that came to mind right off the bat. The system would need a way to know that the notification has already been sent, so as long as you could give it a way to record that, you should be able to accomplish your goal.

 

Hope that helps point you in the right direction!

Hi,

I thought about trying your suggestion, "Change your encoded query so that it only runs when the last_login is equal to the 30 or 60 days, rather than using "<". This could work if the job is running every day and if a last_login value exists. This wouldn't work for users that have already had their last_login time exceeding 60 days.".

 

But I am hesitant as I still feel like this will send the same user an email everyday if they don't login. I only want to send it to them once.

 

I do appreciate your input. I am trying something else and if that does not work, I will be trying your second suggestion of adding a "hidden field".

 

Thanks,

Gina

gurjot5
ServiceNow Employee
ServiceNow Employee

A simple solution would be checking if 'email notification has already been sent for the user' before you trigger the event with the user gliderecord i.e

 

var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('roles=ITIL^active=true^last_login<javascript&colon;gs.beginningOfLast60Days()^ORlast_loginISEMPTY');
gr.query();
while(gr.next()){

 

//check if email has already been sent for the same user lets say in last X days

var emailGR = new GlideRecord('sys_email');

 

emailGR.addEncodedQuery(); //Query should be created at or after X days AND Subject LIKE <ITIL not logged in> AND Recipients Contains gr.user.email_id

 

emailGR.query();

 

//If any email is NOT found for this user in last X days trigger the event

If(!emailGR.next()){

gs.eventQueue("Last.loginX", gr);

}


}

 

Please mark the response as helpful if it resolves your issue