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

Dave,


It is very likely that your solution would work, and I have seen that article in my search. However, my understanding is that the DOM manipulation will not work in Service Portal. Could you verify that for me?


pbo
Mega Expert

Hi



I have the same requirement ie clearing a list_collector( glide_list) when a checkbox field is uncheck.



I tried to put code described in (https://community.servicenow.com/external-link.jspa?url=http%3A//fruitionpartners.eu/blog/2013/11/18... ) in catalog client script (onchange of checkbox field) but this doesn't work



When I uncheck the checkbox field, catalog script is called but doesn't clear the list collector



Any idea ?


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.


Chad,



Thanks for your answer.


I found the 'name' attribute for this variable and added a g_form.clearOptions in my catalog client script.



But I found that when this field is unlock , selected values are still displayed (but not stored as variables in ritm which is good)



I tried to 'unlock' the glide_list variable before the clearOptions but didn't succeed



PS : I saw details in an old article Lock/Unlock glide_list and URL Fields With Client Scripts - ServiceNow Guru but was not able to do the same in catalog request variable set


Patrick,


Here is the code we use to lock/unlock the glide_list:


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


  if (isLoading || newValue == '') {


  return;


  }


  g_form.clearMessages();


  //Type appropriate comment here, and begin script below


  if(newValue == 'Car'){



  //get all elements that are of tag <button>


  var abuttons = document.getElementsByTagName("button");



  //loop through all of the button elements


  for (var ai = 0; ai < abuttons.length; ai++) {



  //get the button element by ID


  var ab = gel(abuttons[ai]);



  //if the button ID contains '_unlock' continue


  if(ab.id.indexOf('_lock') > -1){


  var alockBtn = gel(ab.id);


  var atmpID = ab.id.replace('_lock',"");



  //setup the unlock function


  lock(alockBtn, atmpID,atmpID + '_edit', atmpID + '_nonedit', 'select_0' + atmpID, atmpID + '_nonedit');


  }


  }


  }


  if(newValue == 'Boat'){


  //get all elements that are of tag <button>


  var sbuttons = document.getElementsByTagName("button");



  //loop through all of the button elements


  for (var si = 0; si < sbuttons.length; si++) {



  //get the button element by ID


  var sb = gel(sbuttons[si]);



  //if the button ID contains '_unlock' continue


  if(sb.id.indexOf('_unlock') > -1){


  var sunlockBtn = gel(sb.id);


  var stmpID = sb.id.replace('_unlock',"");



  //setup the unlock function


  unlock(sunlockBtn, stmpID,stmpID + '_edit', stmpID + '_nonedit', 'select_0' + stmpID, stmpID + '_nonedit');


  }


  }


  }


}


This applies to onChange of a dropdown with choices: Car and Boat


If Car is selected, we do not want to allow choices from the Boat glide_list so we lock it. The glide_list does not work the same way in Service Portal so this client script UI Type is Desktop.