Trigger a business rule using a scheduled job
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2022 05:59 PM
Hi guys,
Would like to as your help on how do I trigger a business rule using a scheduled job? or is there any other way to do this?
I just wanted to remove the inactive users from their group. Currently, this business rule is working whenever a user is inactive/deactivated but would like to trigger this by scheduled.
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2022 06:21 PM
Hi
based on your screenshots your Business Rule makes no sense as each time the user data change the respective user is removed from all groups. The reason is that your Business Rules has no conditions.
And to answer your question: Put the code into a Script Include and use the method RemoveUserFromGroups() both in the Business Rule and in the Scheduled Job.
Maik

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2022 06:22 PM
Hello Sabrina,
It is not possible to a BR from a scheduled job.
The right way to do this is to move your logic from the BR to the scheduled job. (So, no BR, just scheduled job).
In your scheduled job, you will have to query for inactive users, and in the while loop, you have the above code. Just make sure to replace current.sys_id with the sysid of the inactive user in the loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2022 07:26 PM
Hi
This is the updated script I am using in Scheduled Job. You said that I will replace current.sys_id with the sysid of the inactive user in the loop?
RemoveUserFromGroups();
function RemoveUserFromGroups() {
var groupMember = new GlideRecord('sys_user_grmember');
groupMember.addQuery('user', current.sys_id);
groupMember.addQuery('active', false);
groupMember.query();
// For each ServiceNow group, delete the membership.
while (groupMember.next()) {
groupMember.deleteRecord();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2022 07:44 PM
Hi
I was able to execute the scheduled job using this script. it worked as expected 🙂
RemoveUserFromGroups();
function RemoveUserFromGroups() {
var groupMember = new GlideRecord('sys_user_grmember');
groupMember.addQuery('user.active', false);
groupMember.query();
// For each ServiceNow group, delete the membership.
while (groupMember.next()) {
groupMember.deleteRecord();
}
}