Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Set value for List Collector

gldecurtins
Mega Expert

Hi all

 

I did write a script which shall do something like "setValue('collectorName', valueArray);" for List Collectors. There is already a script available @ http://www.servicenowguru.com/scripting/client-scripts-scripting/move-list-collector-options/. That script does not work if the selected values are not part of the initial loaded list - this happens if you point the List Collector to a table with many records (> 100 by default).

 

So far I do have got the following code to load a bunch of users to the right side of the List Collector:

function setSysUserCollectorValues(collectorName, selectedOptionsArray) {
       var documentFragment = document.createDocumentFragment();
       for (var i = 0; i < selectedOptionsArray.length; i++) {
               var gr = new GlideRecord('sys_user');
               if (gr.get(selectedOptionsArray[i])) {
                       var option = document.createElement("option");
                       option.text = gr.user_name;
                       option.value = selectedOptionsArray[i];
                       documentFragment.appendChild(option);
               }
       }
       var rightBucket = document.getElementById(collectorName + '_select_1');
       rightBucket.options.length = 0;
       rightBucket.appendChild(documentFragment);
       eval(collectorName + 'acRequest(null)');
}

 

This is the script I use to reset the List Collector:

function resetCollectorValues(collectorName) {
       var leftBucket = document.getElementById(collectorName + '_select_0');
       leftBucket.options.length = 0;
       var rightBucket = document.getElementById(collectorName + '_select_1');
       rightBucket.options.length = 0;
}

 

  • Is there a more elegant way to solve this? I would like to get rid of the fixed glide record to the sys_user table and the eval() function.
  • It looks like the "no_filter" attribute does not work anymore with my code above. Why does it show the filter again after applying these scripts?
  • Is there an out of the box feature to perform this operation?

 

Regards

-Luca.

12 REPLIES 12

So I tested this in a Jakarta Dev instance, and it works, with a catch. If you read the KB article closely you'll see that you have to set the control to work as a Glide List and not as a Slush Bucket.



In my testing, using g_form.setValue('u_user', 'some sys_id') worked as expected and populated the Glide List (note: the KB article shows to use u_user.setValue(), which is incorrect when doing this client-side). It did not work when the control was set as a Slush Bucket.



So, if you're UI requires the Slush Bucket, you're still out of luck 😞



Michael


Hi Michael,



I am trying to copy RITM and have few list collector variables.Values are being copied but getting error" Unhandled exception in GlideAjax".



Have tried steps mentioned in KB article ServiceNow KB: Unable to set the value of a list collector using setValue() (KB0622779)


but still no luck.Also used try & catch method.



Any suggesstion?


An error involving GlideAjax implies you're doing more in your client script than just the call to g_form.setValue()? Without seeing any code, it's near impossible to tell. Try commenting out all code except the g_form.setValue() and see if that works. If it does, start introducing more of your other code to isolate the issue.



Michael