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.

Clear list collector selected values when some field is changed in Service Portal

priteshprajapat
Kilo Contributor

How to clear list collector selected values when some field is changed in Service Portal? Say, I have a list collector on cmdb_ci, and I selected some CIs. Now on change of some filed, i want to clear the already selected values from List collector (in Service Portal). Normal Catalog Item works fine as I cannot use 'gel' in Service Portal, also there is no method in g_list to clear existing values. I already tried Refresh(), and it is not clearing existing values.

3 REPLIES 3

mevenden
Tera Contributor

Hello - I use g_form.clearValue from a client script to remove all selected values from a list collector, i.e.:



g_form.clearValue('my_variable_name');


Hi mevenden,

I'm using the g_form.clearValue method to clear the contents of the list collector field on a catalog item in service portal.  It is working, however you have to click somewhere for the values to actually be cleared from field.  

Did you experience this?

 

carmen87
Tera Contributor

Hi Pritesh


I started looking this article https://www.servicenowguru.com/scripting/client-scripts-scripting/limiting-selections-list-collector...


Then I solved using the code below:



var myListCollector = g_list.get(collectorName);


var selectedOptions = g_form.getValue(collectorName).split(',');


if((selectedOptions != "") && (selectedOptions.length > 0)){


//Remove items


          var index = 0;


          for (var i = 1, leng = selectedOptions.length+1; i < leng; i++){


          myListCollector.removeItem(selectedOptions[selectedOptions.length-i]);


          index++;


          }


}