dvp
Mega Sage
Mega Sage

We had a requirement, where we wanted to display the list of users belonging to a group. This can be done by auto populating the list collector, based on selecting a value in the group reference field.

find_real_file.png

Here's a client script that does it ( Move List Collector Options). This script works beautifully in tool(UI) as well as CMS but not in service portal,   as DOM manipulations in Service Portal are not supported in client script.

After several trails, I decided on using setValue's optional parameter in the client script for this scenario.

Here is the format

g_form.setValue('var_user_groups', sys_id_array, display_value_array);

But the downside of using this is, it is not supported on backend tool. To not let it be a limitation on either screen, I combined both the functionalities in the client script.

Here is the client script

The onChange script is written on a group field which passes the group information to the server side using AJAX.

Server script return the sys_id's and display values of the of users in the group. Once the value is obtained in the client side, sys_id's and display values are separated and set to list collector field.

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

          return;

    }

    //Type appropriate comment here, and begin script below

         

            var grp = g_form.getValue('var_group');  

            var ajax = new GlideAjax('ajaxGetUsersGroups');

            ajax.addParam('sysparm_name', 'getUsers');

            ajax.addParam('sysparm_group', grp);

            ajax.getXML(doSomething);

     

}              

           

        function doSomething(response){

              var answer = response.responseXML.documentElement.getAttribute("answer");

              var arr = answer.split('||');

             

              //arr[1] has the sys_id of the users

              //arr[0] has the display name of the users

             

              if (window === null)

                      g_form.setValue('var_user_groups', arr[1], arr[0]); // Mobile Compatible

            else

                      addItemstoList('var_user_groups', arr[1], arr[0]);  

     

}

function addItemstoList(listCollector, sysid, display_value) {

      var varName = listCollector;

      var leftBucket = gel(varName + '_select_0');

      var rightBucket = gel(varName + '_select_1');

     

      var selectedOptionsIDs = [];

     

      rightBucket.options.length = 0;

     

     

              if (sysid != '') {

              var myCIArray = sysid.split(',');

              var displayvalue = display_value.split(',');

                     

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

                    var option = document.createElement('option');

                    option.value = myCIArray[i];

                    option.text = displayvalue[i];

                    // add the option to the bucket

                    rightBucket.add(option);

              }

      }

      // sort the buckets

      sortSelect(rightBucket);

      moveSelectedOptions(selectedOptionsIDs, leftBucket, rightBucket, '--None--');

     

}

Script Include

Retuns the sys_id and display values of the users

var ajaxGetUsersGroups = Class.create();

  1. ajaxGetUsersGroups.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

        getUsers: function() {

          var group = this.getParameter('sysparm_group');

          var rel = new GlideRecord("sys_user_grmember");

          rel.addQuery("group", group);  

          rel.query();

 

              var name_arr = [];

              var sys_id_arr = [];

               

          while(rel.next()){

                         

                    name_arr.push(rel.user.name.toString());

                    sys_id_arr.push(rel.user.toString());

              }

       

      return name_arr.toString() + '||' + sys_id_arr.toString();        

},        

                                                                                               

});

Previous blogs in this series

Portal diaries: Service Portal — Making Rejection Comments Mandatory on Approval Record

Portal diaries: Service Portal — Approval Summarizer in Portal

26 Comments