User Deactivate after 90, from User table

Hari Babu
Mega Expert

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

1 ACCEPTED SOLUTION

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.

View solution in original post

11 REPLIES 11

Raj68
Mega Guru

Hi hari,

go through below link:

https://community.servicenow.com/community?id=community_question&sys_id=05d20f61dbd8dbc01dcaf3231f9619c0

https://community.servicenow.com/community?id=community_question&sys_id=7c62c7eddb98dbc01dcaf3231f9619b1

NOTE: Mark correct or helpful if it helps you.

Warm Regards,

Raj patel

 

asifnoor
Kilo Patron

Hello Hari,

Create a scheduled job with frequency as Daily and in that check for the users who have not used their account for the last 85 days.

Then trigger 1 event "user_not_logged_in_85_days".

Create a notification which will be fired based on this event and send out a warning mail in that.

Then have another scheduled job (or you can edit the same scheduled job), and trigger another event when the user has not used their account for the last 90 days.

Then trigger 2nd event "user_not_logged_in_90_days".

Create a notification which will be fired based on this event and send out account deactivation mail.

In the same notification, write a mail script which will trigger 3rd event "deactive_user_90_days"

Then write a script action which will listen to this event and deactivate the user here.

The requirement of the 3rd event is, ServiceNow will not send a notification to inactive users. So instead of deactivating, first you send the mail and then you deactivate the user.

I had recently provided code to a similar solution here. Kindly refer to this link

https://community.servicenow.com/community?id=community_question&sys_id=bdc77053db587708190dfb243996...

Mark the comment as a correct answer and also helpful once worked.

Instead of mail script, you can trigger 3rd event along with 2nd event. Just ensure the order of 3rd event is higher and fires only after 2nd event. That way, first notification will go out and then deactivation happens.

Hi asifnoor,

 

Please share me the script As per requirement , I seen your script but i was confused regarding .

 

Please share me script