Add CI SupportGroup to the watchlist automatically when a CI is selected

carlh
Kilo Guru

Hi All,

We have a scenario where some of our app owners are going to need to be aware of incidents.   They don't work the incidents so we don't want to assign them and consume a license.   We do need to give them access to view the incidents that come in about their area though.

When I select a configuration item, the Owner and Support group are also shown on my form.   Based on the Configuration Item selected on the incident, I'd like to add the Owner and Support Group members to the watch list.

Can anyone help me with a script to add a group's members to the watchlist of an incident on insert or maybe when a Boolean field is set to true?

Thanks,

Carl

1 ACCEPTED SOLUTION

curtisr
Kilo Expert

You could create a business rule that adds the owner and group members to the watch list when the incident is created and the values are present on the CI. Haven't tested this specifically, but something similar has worked for me.



Create new business rule: Add CI Owner to watch list


Table: Incident [incident]



When: before


Insert: True


Script:


//Add opened by to the watch list on the requested item if different from the requested for


addToWatchList();


function addToWatchList() {



//get CI owner and ad to watch list


current.watch_list += current.cmdb_ci.owner;



//get group members


var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('group',current.cmdb_ci.support_group);


gr.query();


while(gr.next())


  {


              var user =(','+ gr.user);


              current.watch_list += user;


      }


}


View solution in original post

10 REPLIES 10

Chuck Tomasi
Tera Patron

Hi Carl,



Using this script include: Managing Glide Lists



Create a BEFORE business rule, or other trigger something like this. If you're using a before rule, be sure to put a condition on it like current.cmdb_ci.changes() so it doesn't keep appending the group members on.



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



        var lu = new ListUtil(current.watch_list);


        var groupID = current.cmdb_ci.support_group;


        var grmember = new GlideRecord('sys_user_grmember');


        grmember.addQuery('group', groupID);


        grmember.query();


        while (grmember.next()) {


                  var userID = grmember.getValue('user');


                  if (!lu.isInList(userID)) {


                            lu.addToList(grmember.getValue('user'));


                  }


        }



        current.watch_list = lu.getList();



})(current, previous);


curtisr
Kilo Expert

You could create a business rule that adds the owner and group members to the watch list when the incident is created and the values are present on the CI. Haven't tested this specifically, but something similar has worked for me.



Create new business rule: Add CI Owner to watch list


Table: Incident [incident]



When: before


Insert: True


Script:


//Add opened by to the watch list on the requested item if different from the requested for


addToWatchList();


function addToWatchList() {



//get CI owner and ad to watch list


current.watch_list += current.cmdb_ci.owner;



//get group members


var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('group',current.cmdb_ci.support_group);


gr.query();


while(gr.next())


  {


              var user =(','+ gr.user);


              current.watch_list += user;


      }


}


Hello again,   Sorry it's been so long.   I used the script provided and it worked...or so I thought.



Support tells me there is an issue with line #6 on the above script.



"current.watch_list += current.cmdb_ci.owner;   "



Somehow it's adding "undefined" to the watch list.


I've attached a screenshot to show it.   You will see the group was added.



The owner "Anthony" is in the group.   Could the issue be that we're adding the same person twice in the same Business Rule?





find_real_file.png





Any ideas on this?



Thank you



Carl Helber


Hi Carl,



I doubt that is it, undefined usually means we are calling a variable or value that doesn't exist (or doesn't exist where we are trying to call it from). What I do when I get the undefined is log variations to find the one that I am looking for. Here you are looking for the sys_id of the user "Anthony" to be added to the watch list. Try adding this @ line 4


gs.log("WATCH LIST: "+current.cmdb_ci);


to start with to ensure you are working with the item you are expecting. In certain instances with other fields I have had to use a format like current.variables.cmdb_ci.owner to get my desired sys_id. But I would definitely start at the top level and log it to see exactly what you are working with to figure out where it is failing.



You can find the log under System Logs > System Log > All and message begins with "WATCH LIST" if you are unfamiliar with this approach.