How to populate group members in the dropdown?

varunp
Tera Guru

There is a dropdown in change page, where we need to populate all users listed in one particular group.

Any Idea how to do this?

8 REPLIES 8

Check out this link:


Reference Qualifiers - ServiceNow Wiki



Build a Dynamic Reference Qualifier, then apply it to the field's dictionary entry.




It will be akin to the backfillAssignmentGroup example on that page:




var BackfillAssignmentGroup = Class.create();


BackfillAssignmentGroup.prototype = {


initialize: function() {


},



BackfillAssignmentGroup:function() {


var gp = ' ';


var a = current.assigned_to;



//return everything if the assigned_to value is empty


if(!a)


return;


//sys_user_grmember has the user to group relationship


var grp = new GlideRecord('sys_user_grmember');


grp.addQuery('user',a);


grp.query();


while(grp.next()) {


if (gp.length > 0) {


//build a comma separated string of groups if there is more than one


gp += (',' + grp.group);


}


else {


gp = grp.group;


}


}


// return Groups where assigned to is in those groups we use IN for lists


return 'sys_idIN' + gp;


},


type: 'BackfillAssignmentGroup'


}


I did it on https://demo007.service-now.com/login.do with user/pass admin/admin



I used a client script on Change Request called (empty) and a Script Include called MyFavoritesAJAX.   I now have a choice field on Change that is called Test User Field that populates with the current users of the Database group in the demo instance.



Log in and check it out.



Script Include:


/*


* MyFavoritesAjax script include Description - sample AJAX processor returning multiple value pairs


*/


var MyFavoritesAjax = Class.create();


MyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  /*


  * method available to client scripts call using:


  * var gajax = new GlideAjax("MyFavoritesAjax");


  * gajax.addParam("sysparm_name", "getFavorites");


  */


  getFavorites : function() {


  // build new response xml element for result


  var result = this.newItem("result");



  result.setAttribute("message", "returning all favorites");



  //add some favorite nodes with name and value attributes


  var user = new GlideRecord('sys_user_grmember');


  user.query('group', '287ee6fea9fe198100ada7950d0b1b73');


  user.query();


  while(user.next()){


  this._addFavorite(user.user.name, user.user);


  }




  // all items are returned to the client through the inherited methods of AbstractAjaxProcessor


  },



  _addFavorite : function(name, value) {


  var favs = this.newItem("favorite");


  favs.setAttribute("name", name);


  favs.setAttribute("value", value);


  },



  type : "MyFavoritesAjax"



});



Client Script:


function onLoad() {



  // new GlideAjax object referencing name of AJAX script include


  var ga = new GlideAjax("MyFavoritesAjax");


  // add name parameter to define which function we want to call


  // method name in script include will be getFavorites


  ga.addParam("sysparm_name", "getFavorites");



  // submit request to server, call ajaxResponse function with server response



  ga.getXML(ajaxResponse);



  function ajaxResponse(serverResponse) {


  // get result element and attributes



  var result = serverResponse.responseXML.getElementsByTagName("result");


  var message = result[0].getAttribute("message");



  //check for message attribute and alert user



  if(message)


  alert(message);



  // get favorite elements


  var favorites = serverResponse.responseXML.getElementsByTagName("favorite");


  for(var i = 0; i < favorites.length; i++) {


  var name = favorites[i].getAttribute("name");


  var value = favorites[i].getAttribute("value");


  g_form.addOption('u_test_user_field', value, name);


  }


  }


}


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Varun,



Please go through the below link for more info on how to make a choice list dependent.


Simple way of Dependent Dropdown values in ServiceNow - YouTube


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