Reassign Open tickets to group and remove member from group when user is marked inactive

Atheher Fathima
Mega Guru

Hi All,

 

i have a requirement that whenever a user is marked inactive in sys_user table,

1. we need to check open incidents, tasks, problems and assign them to the group itself(make assigned to blank)

2.For any applications/CI where user is a owner/tech lead - generate a list and trigger notification

3.Remove the user from the group

 

I tried to create a scheduled job to remove members from group and created a BR on sys_user_grmember to reassign the open tasks to group.however i see that it is also removing assigned to for Resolved tickets as well and unable to achieve point 2.

below is my script 

(function executeRule(current, previous /*null when async*/) {
    var groupId = current.getValue("group");
    var groupName = current.group.getDisplayValue();
    var userId = current.getValue("user");
    var userName = current.user.getDisplayValue();

    //look for all Active Task records assigned to the Group AND User
    var grpm = new GlideRecord("task");
    grpm.addEncodedQuery("active=true^assignment_group=" + groupId + "^assigned_to=" + userId);
    grpm.query();
    while (grpm.next()) {
        //add a work note to let people know why the field was cleared
        grpm.work_notes.setJournalEntry("Assigned to field cleared because '" + userName + "' was removed from the current Assignment group '" + groupName + "'");
        grpm.assigned_to = "";
        grpm.update();
    }
})(current, previous);

 

Can someone please help on how i can achieve this with a BR on sys_user table itself on when active changes to false? any help on this will be greatly appreciated

1 ACCEPTED SOLUTION

Martin Ivanov
Giga Sage
Giga Sage

Please mark Correct and Helpful if my answer helps you resolve your issue.

Тhis will close the thread and other users may also benefit from it.

Thanks!
Martin Ivanov
Community Rising Star 2022


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

View solution in original post

10 REPLIES 10

Logan Poynter
Mega Sage
Mega Sage

Hello,

"it is also removing assigned to for Resolved tickets as well" - just tack on another condition in your query for active states (New, WIP, Assigned, Pending). Tasks are active until they are marked Closed.

For the list of CIs in your 2nd bullet, you'd need to establish an empty array and then query cmdb_ci with whatever conditions. Then use a while loop to push the CI name to the array. After, trigger your event passing in the array as a param to be processed by your notification. 


Please mark my answer as correct/helpful if it has helped you.

Thanks,
Logan
--
LinkedIn

it is doing it on resolved cases, because these are still active. they become inactive when they are Closed, so change the encoded query.

Please mark Correct and Helpful if my answer helps you resolve your issue. Thanks!
Martin Ivanov
Community Rising Star 2022


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

You're right! I was editing my answer to address the second bullet and realized I did not explain that - just jumped to filtering it out more by adding on active states only. 

On the second point:

create a business rule on sys_user table, on change of the active field with the following script:

 

var grCI = new GlideRecord('cmdb_ci');
grCI.addQuery('owned_by', current.getUniqueValue());
grCI.query();

var ownedCIs = [];
while (grCI.next()){
    ownedCIs.push(grCI.getValue('name'));
}

gs.eventQueue('your_event_namme', current, ownedCIs.toString());

before doing that - go to event registry and register an event that you will be using. Choose a name of your convinience. Then replace your_event_name with... your event name 😄

next step - go to the notifications, create new, triggered by event, and select your event.

Populate the recipients and subject. In the body type your message and invoke event.parm1.

this will give you string, containing all of the CIs that are owned by this user.

 

Please mark Correct and Helpful if my answer helps you resolve your issue. Thanks!
Martin Ivanov
Community Rising Star 2022


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024