logo

NJP

Portal diaries: Setting List collector values in Service Portal by catalog client script

Import · Oct 20, 2017 · article

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.

image

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 = gform.getValue('vargroup');

var ajax = new GlideAjax('ajaxGetUsersGroups');

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

ajax.addParam('sysparmgroup', grp);

ajax.getXML(doSomething);

}

function doSomething(response){

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

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

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

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

if (window === null)

gform.setValue('varusergroups', arr[1], arr[0]); // Mobile Compatible

else

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

}

function addItemstoList(listCollector, sysid, displayvalue) {

var varName = listCollector;

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

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

var selectedOptionsIDs = [];

rightBucket.options.length = 0;

if (sysid != '') {

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

var displayvalue = displayvalue.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

View original source

https://www.servicenow.com/community/in-other-news/portal-diaries-setting-list-collector-values-in-service-portal/ba-p/2287166