Setting Value On Catalog GlideList

A colleague showed me last week how to turn a List Collector variable (slush bucket) into a GlideList variable like on forms. I don’t know how long this option has been around for [edit: Was introduced with Helsinki] but it’s super easy (and I was very impressed because I’ve wanted to catalog GlideList fields in ages). All you do is create a variable of type List Collector as normal and then set the ‘Variable Attributes’ field to ‘glide_list’.

And that’s all there is to it, navigate to the catalog form and you’ll see the Glide List variable. I thought that was the end of it but then the next step was to populate the variable using a client script. It turns out g_form.setValue doesn’t work as expected. What next then?

Took a bit of digging through the OOB client scripts to find out how ServiceNow adds value to the Glide List. All you have to do is use the following function and arguments:

addGlideListChoice('select_0' + g_form.resolveNameMap('variable_name'),  'sys_id_to_add', 'display_value');However, a small gotcha is this works when the GlideList is in the locked or unlocked position, however when in the lock position it does not update the field values and so looks like it doesn’t work.

The only way to get this working is to first unlock the field, then lock it again.

Looking at how this is handled by ServiceNow, searching the scripts you’ll find:

document.body.on('click', '[data-type="glide_list_unlock"]', function (evt, element) {
var ref = element.getAttribute("data-ref");
unlock(element, ref, ref + "_edit", ref + "_nonedit");
toggleGlideListIcons(ref, false);
var spaceElement = $('make_spacing_ok_' + ref);
if (spaceElement)
spaceElement.style.display = 'none';
toggleAddMe(ref);
evt.stop();
});
You could if you wanted copy all that code and run it manually as part of your client scripts, but the easier option is just to simulate clicking the lock/unlock icon.

The best way to do this would be to add the following code around the script that’s adding the values. So first we ‘unlock’ the GlideList and then after adding the values we ‘lock’ it again:

var ref = g_form.resolveNameMap('variable_name');
$(ref + '_unlock').click();
addGlideListChoice('select_0' + ref,  'sys_id_to_add', 'display_value');
$(ref + '_lock').click();
Update: Thanks Alex for the tip on how to handle in the portal!

This code only works on the native UI and not the portal due to DOM manipulation. However you can update the code as follows to work check whether you’re on the portal or not and then set the field accordingly:

//Set the values you want to use
var sys_id_list = 'sys_id_1,sys_id_2';
var display_name_list = 'display_name_1,display_name_2';
var variable_name = "my_variable";
//window variable will be set to null in the portal
if (window == null) {
g_form.setValue(variable_name ,sys_id_list,display_name_list’);
}
else {
var ref = g_form.resolveNameMap(variable_name);
if(ref){
g_form.clearValue(variable_name )/
$(ref + '_unlock').click();
addGlideListChoice(‘select_0’ + ref, sys_id_list , display_name_list );
$(ref + '_lock').click();
}
}

6 Comments

  1. Pingback: Clearing Values On Catalog GlideList | Service Now Gems

  2. This is fantastic, thanks! We struggled with this issue as well, and wanted to add a little to your script as this works beautifully for native ui but, not for SP or CSM.

    //first, set the variable for SP
    var sys_id_list = ‘sys_id_1,sys_id_2,sys_id_3,…’;
    var display_name_list = ‘display_name_1,display_name_2,…’;
    g_form.setValue(‘variable_name’,sys_id_list,display_name_list’);

    //Since SP doesn’t read the information below, it gets ignored
    //If ServiceNow does read it, we can assume that
    //this code is in native_ui, so we want to clear the variable
    //we just set for the SP

    var ref = g_form.resolveNameMap(‘variable_name’);
    if(ref){ //checks to see if there is a value in ref
    g_form.clearValue(‘variable_name’)/
    $(ref + ‘_unlock’).click();
    addGlideListChoice(‘select_0’ + ref, ‘sys_id_to_add’, ‘display_value’);
    $(ref + ‘_lock’).click();
    }

    This tweak will help make that glide_list manageable in both native ui and portals. I’m sure there are better way to check for portal view, but wanted to give you the concept for setting that list collector value on the portal side!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s