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

dalton1
Giga Expert

Hi Gian Luca Decurtins,



Have you solved this issue of yours? Apparently, I have the same business case. I need to populate the Selected Values section of the List Collector with predefined options. Anyone who could help us out? Would greatly appreciate it.


Hi Dalton



This is my final code:


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


      setCollectorFilter('var_list','u_application_id=' + newValue);


     


      if (!isLoading) {


              moveCollectorValues('var_list', newValue);


      }


}



function setCollectorFilter(collectorName, filterString) {


      g_form.setDisplay(collectorName, false);



      if (eval('typeof(' + collectorName + 'g_filter)') == 'undefined' ) {


              setTimeout(function() { setCollectorFilter(collectorName,filterString); }, 100);


              return;


      }



      eval(collectorName + 'g_filter.reset()');


      eval(collectorName + 'g_filter.setQuery("' + filterString + '")');


      eval(collectorName + 'acRequest(null)');



      g_form.setDisplay(collectorName, true);


}



function moveCollectorValues(collectorName, selectedOptionsArray) {


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


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


      var availableOptionsArray = leftBucket.options;


      var selectedOptionsIDs = [];


      var i = 0;


      if (availableOptionsArray.length != 0) {


              for (var j = 0; j < selectedOptionsArray.length; j++) {


                      for (var k = 0; k < availableOptionsArray.length; k++) {


                              if (selectedOptionsArray[j] == availableOptionsArray[k].value) {


                                      selectedOptionsIDs[i] = k;


                                      i++;


                              }


                      }


              }


      }


      rightBucket.options.length = 0;


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


}



Hope this helps.



Regards


-Luca.


Hi Gian Luca Decurtins



Can you explain below function further?



Does this mean that items will be moved to the right bucket? What is the 2nd, 3rd and 4th parameter for?



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





What if I only want the currently logged in user to be in the Selected list bucket on page load? How will I do that? Apparently, I wont be coding it in the OnChange client script