Automatically Close All Incidents When a User Becomes Inactive

yashhaval45
Giga Contributor

Hi everyone,

I have a requirement in ServiceNow.

Requirement:
When a user is marked as inactive (Active = false) in the User (sys_user) table, all incidents created by that user should automatically be moved to the Closed state.

Example:
Suppose a user has created 5 incidents. When an administrator deactivates that user, all 5 incidents created by that user should automatically be closed.

I have the following questions:

1. Which type of Business Rule should I use for this requirement?
2. Should I create the Business Rule on the User (sys_user) table or the Incident (incident) table?
3. How can I retrieve all incidents created by that user and update their state to Closed?
4. What is the recommended approach to implement this requirement?

I would appreciate any suggestions or guidance.

Thank you!

3 REPLIES 3

Hemanth M
Tera Sage

Hi @yashhaval45 ,

 

I hope you are okay with closing rather resolving.

 

There are a couple of ways to do this. This approach is transactional (not scheduled) and requires no scripting:

  1. Create a Flow Designer flow on the User table.

  2. Set the trigger conditions: Active changes, AND Active changes to false.

  3. Add a Look Up Records action on the Incident table where the Caller is the user and Active is true (Note: this will also include resolved incidents).

  4. Add a For Each logic block for the looked up records. Update the incident State to Closed, making sure to also populate the mandatory fields or work notes

  5. End the flow.

 

Example: 

HemanthM_0-1792502950420.png

 

Hope helps!

Hemanth M
Tera Sage

Hi @yashhaval45 ,

 

If this response helped, please close the loop by accepting the solution or ask a follow-up question.

 

 

 

sonalibhaga
Tera Contributor
The cleanest implementation is to trigger the automation when the user becomes inactive, so the Business 1. 1. Rule belongs on sys_user, not incident.
2. Use an After Update Business Rule on the User [sys_user] table.
Why After? The action you care about is the successful change of the user's active value, and then you want to modify separate Incident records.
3.  The trigger is: User changes from Active → Inactive
 
(function executeRule(current, previous /*null when async*/) {
// Run only when the user is changed from active to inactive
if (current.active.changesTo(false)) {
var inc = new GlideRecord('incident');
// sys_created_by stores the username
inc.addQuery('sys_created_by', current.user_name);
// Don't process incidents that are already closed
inc.addQuery('active', true);
inc.query();
while (inc.next()) {
inc.setValue('state', 7); // Closed in standard Incident
inc.update();
}}
})(current, previous);
 

Closing an Incident usually isn't just a matter of setting state.

Your Incident configuration may have mandatory closure fields such as:

  • Close code
  • Close notes
  • Resolution information
while (inc.next()) {
inc.setValue('state', 7);
inc.setValue('close_code', '...');
inc.setValue('close_notes',
'Automatically closed because the creating user was deactivated.');
inc.update();
}
 
Accept and hit Helpful if it helps.

Thank you,
Sonali Bhagat