
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2018 05:08 PM
Hi All,
I'm trying to implement some code to automatically set the assignee of an Incident. I have created a Before Business Rule called 'Set Assignee to Auto-Follow', on the 'Incident' table, run on 'insert' and 'update', order 100, condition 'active=true'. Code for this is below:
(function executeRule(current, previous /*null when async*/) {
//Auto add user as a follower of record
var gp = new GlideRecord('live_group_profile');
gp.addQuery('document', current.sys_id);
gp.addQuery('table', current.getTableName());
gp.query();
if (gp.next()) {
gs.info('Profile record == ' + gp.short_description);
addMember(gp.sys_id, current.assigned_to);
}
function addMember(id, user) {
//first need to check if assigned to user has live feed profile
var lpUser = checkLiveFeedProfile(user);
//now to check to see if user is in group already, if not, add
var gr = new GlideRecord('live_group_member');
gr.addQuery('group.sys_id', id);
gr.addQuery('member.sys_id', lpUser);
gr.query();
if (gr.next() && (gr.state == 'active' || gr.state == 'admin')) { //need to make sure user wasn't already following.
gs.info('user =' + gr.member.name + ' already a member as ' + gr.state);
} else {
//this will handle if user was in group already but inactive(meaning they unfollow record) and will just create member if not in group already
gs.info('User = ' + user.name + ' was added/reactivated to live group');
gr.state = 'active';
gr.group = id;
gr.member = lpUser;
gr.update();
}
}
function checkLiveFeedProfile(user) {
var lp = new GlideRecord('live_profile');
lp.addQuery('document', user);
lp.query();
if (lp.next()) {
//user already has live profile
gs.info('User ' + lp.name + ' had an live feed account');
return lp.sys_id;
} else {
//create one
gs.info('user ' + user.name + ' did not have live feed profile account. creating now...');
lp.table = 'sys_user';
lp.document = user.sys_id;
lp.type = 'user';
lp.name = user.name;
return lp.update();
}
}
})(current, previous);
The code was used from another post which is Make a user a follower via a script - but I can't get it working - although others appear to have it working successfully...
The debug below shows it's running, see below - but when I impersonate the user assigned to a ticket they aren't automatically set to 'following'.
13:51:14.103: Global ==> 'Set Assignee to Auto-Follow' on incident:INC0044360
13:51:14.104: Execute before query business rules on live_group_profile:
13:51:14.104: Global ==> 'LiveFeed Group Profile Visibility' on live_group_profile:
13:51:14.104: Global <== 'LiveFeed Group Profile Visibility' on live_group_profile:
13:51:14.104: Global ==> 'LiveFeed Group Profile Visibility 2.0' on live_group_profile:
13:51:14.105: Global <== 'LiveFeed Group Profile Visibility 2.0' on live_group_profile:
13:51:14.105: Finished executing before query business rules on live_group_profile:
13:51:14.107: Global <== 'Set Assignee to Auto-Follow' on incident:INC0044360
I'm hoping I've just forgotten something simple - any help is appreciated!
I will also need to do this for HR Cases, but once I have the above working I'll be able to transfer this across.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2018 01:55 PM
Hi Carl,
Made one update to the script. Please try this and let me know if it works.
(function executeRule(current, previous /*null when async*/) {
//Auto add user as a follower of record
var gp = new GlideRecord('live_group_profile');
gp.addQuery('document', current.sys_id);
gp.addQuery('table', current.getTableName());
gp.query();
if (gp.next()) {
gs.addInfoMessage('Profile record == ' + gp.short_description);
addMember(gp.sys_id, current.assigned_to);
}
else
{
gp.initialize();
gp.setWorkflow(false);
gp.name = current.number;
gp.short_description = current.short_description;
gp.document = current.sys_id;
gp.document_group = "true";
gp.table = current.getTableName();
gp.visible_group = "false";
gp.public_group = "false";
var gpid = gp.insert();
addMember(gpid, current.assigned_to);
}
function addMember(id, user) {
//first need to check if assigned to user has live feed profile
var lpUser = checkLiveFeedProfile(user);
//now to check to see if user is in group already, if not, add
var gr = new GlideRecord('live_group_member');
gr.addQuery('group.sys_id', id);
gr.addQuery('member.sys_id', lpUser);
gr.query();
if (gr.next() && (gr.state == 'active' || gr.state == 'admin')) { //need to make sure user wasn't already following.
gs.addInfoMessage('user =' + gr.member.name + ' already a member as ' + gr.state);
} else {
//this will handle if user was in group already but inactive(meaning they unfollow record) and will just create member if not in group already
gs.addInfoMessage('User = ' + user.name + ' was added/reactivated to live group');
gr.state = 'active';
gr.group = id;
gr.member = lpUser;
gr.update();
}
}
function checkLiveFeedProfile(user) {
var lp = new GlideRecord('live_profile');
lp.addQuery('document', user);
lp.query();
if (lp.next()) {
//user already has live profile
gs.addInfoMessage('User ' + lp.name + ' had an live feed account');
return lp.sys_id;
} else {
//create one
gs.addInfoMessage('user ' + user.name + ' did not have live feed profile account. creating now...');
lp.table = 'sys_user';
lp.document = user.sys_id;
lp.type = 'user';
lp.name = user.name;
return lp.update();
}
}
})(current, previous);
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2019 09:51 AM
I created some functions using some examples from this post and posted it on Share if you want to check it out. This week I just rewrote the whole thing to add more functions and abilities like using a service account to send messages to users without the need for a related record.
https://developer.servicenow.com/app.do#!/share/contents/5628973_alert_utils?t=PRODUCT_DETAILS
Eric LeMonnier

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2019 03:32 PM
FYI after using this for some time, we found that a number of our ITIL users wouldn't remove themselves from the live feed when the incident was resolved and just leaving it in their connect chat list. This was slowing down the instance significantly, particularly for those users.
I made a small script to remove users from live groups if the incident was closed or cancelled and set it as a scheduled job to run once a week. this has improved performance quite a bit.
I also included requests because a few people use live feed to communicate with users in requests too.
//finds live group memberships to closed incidents and deletes them.
var gp = new GlideRecord('live_group_member');
gp.addEncodedQuery('memberISNOTEMPTY');
gp.query();
while (gp.next()){
var doc = new GlideRecord('incident');
doc.get(gp.group.document);
if (doc.state == "7" || doc.state == "8"){
gs.log("Live Group Member record " + gp.group.name + " is being removed as the incident is closed. Member is " + gp.member.name,'DEBUG');
gp.deleteRecord();
}
var req = new GlideRecord('sc_request');
req.get(gp.group.document);
if (req.request_state == 'closed_complete'){
gs.log("Live Group Member record " + gp.group.name + " is being removed as the request is closed. Member is " + gp.member.name,'DEBUG');
gp.deleteRecord();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2022 09:22 AM
Hi, What changes does the chat record when following the record? would it be possible to configure it so that it generates notification with any update in the registry?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2022 02:37 PM
Hi Alejandra
If you are following the record on the live feed, it will show any comments or work notes in the connect sidebar chat.
You could write an onchange business rule so that if the record is updated in (whatever way you wish to be notified of), it populates the work notes with something. I have something like this for a request we have, where the vendor replies to an RITM notification but we want the parent request to show a notification in the connect chat for the user who is following the feed.
example might be if the priority changes from low to high, the business rule updates the work notes with something like "priority change to (new value) - please review". The ITIL user following the feed will get this in their connect chat.