How to clear the contents of a glide list using a client script

chadlockwood
Kilo Sage

I have a checkbox and a list collector(glide_list) on a catalog item. if the checkbox is checked(true), the glide_list is visible(via UI policy). If the requestor adds items to the glide_list but then unchecks the checkbox, I want to clear the contents of the glide_list.

In the JavaScript Executor, I have run the following:

var a = g_form.getValue('IO:81332e394f2ae200b8ec7f75f110c77e');

alert(a);

var b = [];

g_form.setValue('IO:81332e394f2ae200b8ec7f75f110c77e', b);

Lines 1&2 successfully collect the contents of the glide_list and display them as a comma-separated list of sys_ids, as expected. Lines 3&4 appear to successfully set the value of the field to an empty string, however, the display values remain in the field. If I run lines 1&2 again, the alert appears to return nothing.

Additionally, I have an onSubmit client script that shows an error message if the checkbox is checked and the glide_list is empty. If I run the above code and click Submit, the error message appears, as if the glide_list is empty, even though the display values remain.

Is there another way to clear the contents of a glide_list using a client script? Or do I need to find some way to re-render the glide_list after I've cleared it out?

1 ACCEPTED SOLUTION

Patrick,


Here is what I did:


First, open your catalog item, right-click the window portion that holds the items you select, then click Inspect.(assuming you are using Chrome)


Look for the "name" attribute, should look something like:


name="select_0IO:81332e394f2ae200b8ec7f75f110c77e"


Copy the value of the name attribute, including "select_".


Add this code to your script to clear the values:



g_form.clearValue('<variable_name>');


g_form.clearOptions('select_0IO:71332c394e2ae212b8ec6f65f110c66e');//replace with your "select_" value



I believe the system technically recognizes the glide_list as two separate fields which is why both lines are necessary.



Hope this helps.


View solution in original post

17 REPLIES 17

Chad



Thanks for sharing this code.



I still have question regarding glide_list.


I would like to initialize selected values in a glide list (say glideListVar which is a reference to sys_user) based on another form element.



I created a on onChange catalog script on var1 (string) and tried


  var testId='97de070d3750ba007deb98a543990ebb'; // sys_id of User 1


  var otherId='7140c1b237fba2007deb98a543990e4b';// sys_id of User 2


  if (newValue == 'Test' ) {


  g_form.setValue('glideListVar',testId);


  } else {


g_form.setValue('glideListVar',otherId);


}



I also tried to use g_form.setValue('select_0IO:7140c1b237fba2007deb98a543990e4b',testID);



Capture d



But none of these works


Patrick,


This may work in the Advanced view:


http://www.servicenowguru.com/scripting/client-scripts-scripting/move-list-collector-options/



It will not work in Service Portal, however, because of the Jelly/DOM manipulation bits.


Will this also work for glide_list (I will try tomorrow morning)



I believe that in this case (glide_list) , varName + '_select_1' or varName + '_select_0' don't exist


Here I wrote a quick blog post that includes how to handle this in the service-portal, as chadlockwood has pointed, the DOM stuff doesn't work in service-portal. There is actually a much better way, just see my blog post here: How to set/clear a list collector in the catalog .


I 'm still looking for a way to set a glide_list variable from client script.



I found a way clear the variable   (thanks to chadlockwood) with


g_form.setValue('perimetre','');


g_form.clearOptions('select_0IO:c7293ecbdbdc3240951a70d9bf961940');


where IO:c7293ecbdbdc3240951a70d9bf961940 is found with Inspect (Chrome)



If glide_list variable is locked, we still see old values, but it is not stored and also disapear when we try to unlock glide_list



But I need to force value of this variable from a catalog client script


I tried g_form.setValue('perimetre', value) but this doen't work



Any idea ?



Thanks