Code to set Assignee to 'Auto-follow' when assigned to an Incident

Carl Fransen1
Kilo Sage

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.

1 ACCEPTED SOLUTION

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);


View solution in original post

28 REPLIES 28

Okay no worries, thank you for the replies!

Hi Sanjiv,



Apologies this doesn't quite work as yet - it adds the other person correctly but it ALSO adds the person who updated the ticket as a follower - which is incorrect as we only want the assignee added automatically not the initiator of the assignment.


Can you help to remove this from the code?



Thanks


I just removed that part of that code. I will see if there is an option to add that message to tracking.



(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.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);


Hi Sanjiv,


Thanks for your quick replies - I think our messages were crossed so will explain below.



Firstly - The original code you provided worked to add the assignee as a 'follower' BUT it also adds the person adding the assignment (so the person who updates the record) as a 'follower' - this last behaviour is incorrect.   I'm not sure why it's adding both the assignee AND the person updating the incident - if you could have a look that would be wonderful.



Secondly the code to add the message that the assignee has been added to the group - is optional, but appreciate you looking into this.



The first issue is the priority as I can't use this in production until that is solved.



Thanks.


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);