Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

how to see if updated by is member of a group

mduluk
Giga Expert

We are trying to have a SLA where if the service desk incident is updated by some one other then the service desk it will start a two hour timer.   The SLA would stop when the Service Desk updates the ticket or it is closed/transferred.\

 

The problem is updated by is a string.   I have a script include that returns an array of all the service desk usernames, but it looks like the filter is reading the whole comma separated arraty, not each element as a different user name.   Any ideas?

 

function getServiceDeskUserNames(){

     

      var arrayUtil = new ArrayUtil();

      var groupMembers = [];

      var grGroupMembers = new GlideRecord('sys_user_grmember');

      //get the service desk group

      grGroupMembers.addQuery('group','9d4cb8b538494900a06b750aa778bbcc');

      grGroupMembers.query();

      while (grGroupMembers.next()) {

              groupMembers.push(grGroupMembers.user.user_name.toString());

      }

return arrayUtil.unique(groupMembers);

}

1 ACCEPTED SOLUTION

This is untested but you get the general idea...   Just ensure you change lines 2 and 3 to match your environment and please mark the post as answered if this works out



function getServiceDeskUserNames() {


  var user = current.sys_updated_by;


  var groupName = "Service Desk";



  //Get Users sys_id


  var usrGr = new GlideRecord('sys_user');


  if(usrGr.get('user_name', user) {


  userID = usrGr.sys_id + '';


  }


  else {


  //we didn't find the user return false


  return false;


  }




  //get Group sys_id


  var grpGr = new GlideRecord('sys_user_group');


  if(grpGr.get('name', groupName)) {


  groupID = grpGr.sys_id + '';


  }


  else {


  //we didn't find the group return false


  return false;


  }




  //Now see if they are a member of the group


  var grpMbr = new GlideRecord('sys_user_grmember');


  grpMbr.addQuery('user', userID);


  grpMbr.addQuery('group', groupID);


  gr.query();


  if (gr.next()) {


  return true;


  }


  return false;


}


View solution in original post

6 REPLIES 6

Marcus Fly
Tera Expert

Why not use the OOB ServiceNow isMemberOf() function?


http://wiki.servicenow.com/index.php?title=Getting_a_User_Object#Getting_a_User_Object



var ourUser = gs.getUser();


ourUser. ­isMemberOf( ­'Capacity Mgmt') ;


mduluk
Giga Expert

The problem is I am not checking against the current logged in user.   The SLA is just running on the system after the update.   And the field I am trying to check is a text field, not a reference field.   What I need is to use the updated by field to look up a user by user name and then see if they are a member of the group, it think.


This is untested but you get the general idea...   Just ensure you change lines 2 and 3 to match your environment and please mark the post as answered if this works out



function getServiceDeskUserNames() {


  var user = current.sys_updated_by;


  var groupName = "Service Desk";



  //Get Users sys_id


  var usrGr = new GlideRecord('sys_user');


  if(usrGr.get('user_name', user) {


  userID = usrGr.sys_id + '';


  }


  else {


  //we didn't find the user return false


  return false;


  }




  //get Group sys_id


  var grpGr = new GlideRecord('sys_user_group');


  if(grpGr.get('name', groupName)) {


  groupID = grpGr.sys_id + '';


  }


  else {


  //we didn't find the group return false


  return false;


  }




  //Now see if they are a member of the group


  var grpMbr = new GlideRecord('sys_user_grmember');


  grpMbr.addQuery('user', userID);


  grpMbr.addQuery('group', groupID);


  gr.query();


  if (gr.next()) {


  return true;


  }


  return false;


}


Try using the below method to compare the value in a array and in a string variable you have


var grGroupMembers = new GlideRecord('sys_user_grmember');


      //get the service desk group


      grGroupMembers.addQuery('group','9d4cb8b538494900a06b750aa778bbcc');


      grGroupMembers.query();


      while (grGroupMembers.next()) {


        for (i=0; i<(grGroupMembers.user.user_name.length())-1;i++)


        {


                  If grGroupMembers.user.user_name(i).toString() = "XYZ"


                                      return True;


        }


        }



Worth to give a try as i haven't tested the above script.