Add Opened by User to Watch List

mfoster817
Tera Contributor

We have a request that when an incident is created and needs to be escalated to another assignment group, that the opened by user is not part of, to add the opened by user to the watch list.

 

It is partially working, but it always adds the opened by user to the watch list regardless if they are part of the group selected or not. 

 

We created a business rule to Add Opened by user to watch list

  • When to run
    • Before
    • Insert/update
  • Advanced
    •  Condition
      • current.assignment_group.changes() && !current.assignment_group.getMembers().contains(current.opened_by)
    • Script
      • (function executeRule(current, previous /*null when async*/) {
        // Check if assignment group has changed
        if (current.assignment_group.changes()) {
        var assignmentGroup = current.assignment_group.getRefRecord();
        var openedByUser = current.opened_by.getRefRecord();

        // Check if the opened by user is not a member of the new assignment group
        if (assignmentGroup && openedByUser && !assignmentGroup.getMembers().contains(openedByUser)) {
        // Add the opened by user to the watch list
        current.watch_list = current.watch_list + ',' + current.opened_by;
        }
        }
        })(current, previous);

What are we missing? 

Thank you!

 

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

Hi @mfoster817  You can try to use below script in business rule, that will solve your requirement. 

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    if ((current.watch_list.indexOf(current.opened_by)== -1 || current.watch_list.isNil()) && !gs.getUser().getUserByID(current.opened_by).isMemberOf(current.assignment_group))
        current.watch_list +=  ","+current.opened_by;

})(current, previous);

 

Note: Just set only one filter condition on your business rule  < Assignment Group > <Changes>

 

Hope it will help you.

 

Thanks,

Harsh

View solution in original post

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

I dont think you can use this !current.assignment_group.getMembers().contains(current.opened_by)

 

Instead you can query the User Group table and get the assignment.

 

  • (function executeRule(current, previous /*null when async*/) {
    // Check if assignment group has changed
    if (current.assignment_group.changes()) {
    var assignmentGroup = new GlideRecord('sys_user_group');
    assignmentGroup.addQuery('group',current.assignment_group);

    assignmentGroup.addQuery('user',current.opened_by);

    assignmentGroup.query();

    // Check if the opened by user is not a member of the new assignment group
    if (assignmentGroup.next()) {
    // Add the opened by user to the watch list
    current.watch_list = current.watch_list + ',' + current.opened_by;
    }
    }
    })(current, previous);

 


Please mark this response as correct or helpful if it assisted you with your question.

Harsh Vardhan
Giga Patron

Hi @mfoster817  You can try to use below script in business rule, that will solve your requirement. 

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    if ((current.watch_list.indexOf(current.opened_by)== -1 || current.watch_list.isNil()) && !gs.getUser().getUserByID(current.opened_by).isMemberOf(current.assignment_group))
        current.watch_list +=  ","+current.opened_by;

})(current, previous);

 

Note: Just set only one filter condition on your business rule  < Assignment Group > <Changes>

 

Hope it will help you.

 

Thanks,

Harsh

DpkSharma
Giga Expert

Hi @mfoster817 

  1. Instead of concatenating the current.opened_by value to the current.watch_list using the + operator, the push() method is used to add the current.opened_by value to the current.watch_list array.

 

(function executeRule(current, previous /*null when async*/) {
  // Check if assignment group has changed
  if (current.assignment_group.changes()) {
    var assignmentGroup = current.assignment_group.getRefRecord();
    var openedByUser = current.opened_by.getRefRecord();

    // Check if the opened by user is not a member of the new assignment group
    if (assignmentGroup && openedByUser && !assignmentGroup.getMembers().contains(openedByUser)) {
      // Add the opened by user to the watch list
      current.watch_list.push(current.opened_by);
    }
  }
})(current, previous);

Please Mark this Helpful and Accepted. If this solves your query
Regards,
Deepak Sharma