Populate list collector variable with groups from user variable

dagarson
Tera Guru

Hello I've been struggling with trying to populate a list collection of all the groups the user is in based on what user is selected in a reference variable. here is my code

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   var userRefField = g_form.getReference('user', onUserRefLoaded);

   function onUserRefLoaded(userRef) {
      if (!userRef) {
         return;
      }

      var groupList = userRef.getMyGroups();
      var groupArray = [];

      while(groupList.next()) {
         var groupName = groupList.name;
         groupArray.push(groupName);
      }

      var lc = g_form.getControl('groups');
      lc.clearOptions();

      for (var i = 0; i < groupArray.length; i++) {
         lc.addOption(groupArray[i], groupArray[i], i);
      }
   }
}

It runs onChange of the user reference variable.

The goal is to basically have the groups populate based on the user added but I cant seem to get it working 

dagarson_0-1679847842432.png

Any assistance would be appreciated. 

1 ACCEPTED SOLUTION
3 REPLIES 3

Roshan Tiwari
Tera Guru

Hi @dagarson ,

 

Please use GlideAjax instead of getReference. 

Refer this link:

Extending GlideAjax | ServiceNow Developer

 

Hi @dagarson ,

 

ServiceNow won't suggest GlideRecord in Client side scripting better you use same GlideRecord in Script include and call it from client script.