- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2019 11:09 PM
Hi ,
I would like to send the notification to the user do not using their account after 90 days, The idea would be I think to send a first notification 85 days to warn him and and a second the 90th day to informed him that the account will be disabled from this day. is it possible to put us in copy of his mails? is it possible that they are disabled automatically by service now.
Regards,
Hari
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2019 12:33 AM
Here is how you do it.
1. First, create the 3 events as i have mentioned. Create all 3 on table sys_user.
2. Navigate to scheduled jobs and create New and select "Automatically run a script of your choosing" and select Daily under Run filed.
Name: Notify users 85 days
In the script, place the below code
var gr = new GlideRecord("sys_user");
gr.addActiveQuery();
gr.addQuery("last_login",'<=',gs.daysAgo(85));
//If you want to include users whose last login is null, then replace the above add query with this
//gr.addQuery("last_login",'<=',gs.daysAgo(85)).addOrCondition("last_login",'');
gr.query();
while(gr.next()) {
gs.eventQueue('user_not_logged_in_85days_ago', gr, gr.getValue("email")); //ensure the event name matches correctly.
}
Create another Scheduled job similar to above and name it
Notify users 90 days and deactivate
Under script, place the below code
var gr = new GlideRecord("sys_user");
gr.addActiveQuery();
gr.addQuery("last_login",'<=',gs.daysAgo(90));
//If you want to include users whose last login is null, then replace the above add query with this
//gr.addQuery("last_login",'<=',gs.daysAgo(90)).addOrCondition("last_login",'');
gr.query();
while(gr.next()) {
gs.eventQueue('user_not_logged_in_90days_ago', gr, gr.getValue("email")); //ensure the event name matches correctly.
//trigger deactive user event.
gs.eventQueue('deactivate_user', gr, gr.getValue("sys_id")); //ensure the event name matches correctly.
}
Save both scheduled jobs and run once by clicking Execute Now and go to event log and confirm that youhavee the events triggered or not. If the events are shown in event log, then scheduled job is working fine.
3. Now go to notifications and create 2 notifications on table sys_user.
a. One for the 85 days notification. In that under Send when, select Event is fired and select the 85 days event name. click on Advanced view.
Then under who to receive, select "Event parm 1 contains recipient"
under What it contains, write your notification message
b. Follow the same procedure for 90 days event. In that select the 90 days event under event name.
4. Once this is done, now go to Script Actions (system policy -> events -> script actions) and click new
Name: Deactive User
Event: Select the deactivate event name here
Keep the execution order something like 500
Select Active
Under script, put the following code
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id",event.parm1);
//gr.addQuery("sys_id", "8ff5b254b33213005e3de13516a8dcf7");
gr.query();
if (gr.next()) {
gs.print("User is deactivated"+gr.user_name);
gr.active=false;
gr.update();
}
Mark the comment as a correct answer and my other comment(s) as helpful once worked.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2019 12:33 AM
Here is how you do it.
1. First, create the 3 events as i have mentioned. Create all 3 on table sys_user.
2. Navigate to scheduled jobs and create New and select "Automatically run a script of your choosing" and select Daily under Run filed.
Name: Notify users 85 days
In the script, place the below code
var gr = new GlideRecord("sys_user");
gr.addActiveQuery();
gr.addQuery("last_login",'<=',gs.daysAgo(85));
//If you want to include users whose last login is null, then replace the above add query with this
//gr.addQuery("last_login",'<=',gs.daysAgo(85)).addOrCondition("last_login",'');
gr.query();
while(gr.next()) {
gs.eventQueue('user_not_logged_in_85days_ago', gr, gr.getValue("email")); //ensure the event name matches correctly.
}
Create another Scheduled job similar to above and name it
Notify users 90 days and deactivate
Under script, place the below code
var gr = new GlideRecord("sys_user");
gr.addActiveQuery();
gr.addQuery("last_login",'<=',gs.daysAgo(90));
//If you want to include users whose last login is null, then replace the above add query with this
//gr.addQuery("last_login",'<=',gs.daysAgo(90)).addOrCondition("last_login",'');
gr.query();
while(gr.next()) {
gs.eventQueue('user_not_logged_in_90days_ago', gr, gr.getValue("email")); //ensure the event name matches correctly.
//trigger deactive user event.
gs.eventQueue('deactivate_user', gr, gr.getValue("sys_id")); //ensure the event name matches correctly.
}
Save both scheduled jobs and run once by clicking Execute Now and go to event log and confirm that youhavee the events triggered or not. If the events are shown in event log, then scheduled job is working fine.
3. Now go to notifications and create 2 notifications on table sys_user.
a. One for the 85 days notification. In that under Send when, select Event is fired and select the 85 days event name. click on Advanced view.
Then under who to receive, select "Event parm 1 contains recipient"
under What it contains, write your notification message
b. Follow the same procedure for 90 days event. In that select the 90 days event under event name.
4. Once this is done, now go to Script Actions (system policy -> events -> script actions) and click new
Name: Deactive User
Event: Select the deactivate event name here
Keep the execution order something like 500
Select Active
Under script, put the following code
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id",event.parm1);
//gr.addQuery("sys_id", "8ff5b254b33213005e3de13516a8dcf7");
gr.query();
if (gr.next()) {
gs.print("User is deactivated"+gr.user_name);
gr.active=false;
gr.update();
}
Mark the comment as a correct answer and my other comment(s) as helpful once worked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2019 02:15 AM
Thanks for your Support ,
Need Small fever How can you take this line
in user table filter condition .
("last_login",'<=',gs.daysAgo(85));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2019 02:24 AM
In the user table filter, its a fixed list.
So take 90 days filter or 60 days filter. and then manually verify.
or select the date which is 85 days behind the current date and then checks the users.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2019 02:52 AM
Thank you

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2019 03:05 AM
Hi,
Kindly mark my comment as a correct answer and also my other comment(s) as helpful once worked.