The CreatorCon Call for Content is officially open! Get started here.

Setting List Collector Selections in a Script

Mike McCall
Giga Guru

I've seen a lot about how to default the filter for a list collector (pre-filling the "left side"), but I can't seem to find anything about defaulting the selected values of a list collector (to pre-fill the "right side"). The closest I came was unfortunately a dead end: How to pre-populate a collection list?

In that post, it was suggested that you should be able to set list collector selections by using the standard g_form.setValue() method (http://wiki.servicenow.com/index.php?title=GlideForm_%28g_form%29#setValue), but I can't get that to work:


// This should work, according to the Wiki:
g_form.setValue('watch_list', '1', 'Michael McCall'); // assuming that '1' were actually my sys_id value

// However, having this in a catalog client script does not work:
g_form.setValue('requested_users', '1', 'Michael McCall'); // assuming that I have a list collector variable named "requested_users"

Is it just not possible to programmatically set list collector values, or is there a special method for doing so? (I might guess it has something to do with the g_filter object, but I can't find any information about that object beyond the setFilter() method.)
1 ACCEPTED SOLUTION

Jace Benson
Mega Sage

List collector's are very different.

I think Mark Stanger's post is probably the easiest to follow;
http://www.servicenowguru.com/scripting/client-scripts-scripting/move-list-collector-options/
the tl;dr is that there are two functions available


moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--')
and

sortSelect(leftBucket)

These should let you move stuff around, and re-sort the left or right objects.


View solution in original post

9 REPLIES 9

Yogesh ,




PFB.


Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.



//Name of variable to move options from
var varName = 'YOUR_VARIABLE_NAME_HERE';
var leftBucket = gel(varName + '_select_0');
var rightBucket = gel(varName + '_select_1');
var selectedOptions = rightBucket.options;



//Get an array of all option IDs to move
var selectedIDs = new Array();
var index = 0;
for(var i = 0; i < selectedOptions.length; i++){
      selectedIDs[index] = i;
      index++;
}



//Move all returned options from right to left bucket and sort the results
//Switch 'rightBucket' and 'leftBucket' to move from left to right
moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--');
//Sort the resultant options in the left bucket
sortSelect(leftBucket);


Community Alums
Not applicable

what is this _select_0 and _select_1;


Community Alums
Not applicable

this ain't working .. i put user table on list collector to autopopulate current logged in user on left side of list collector. please help me on this.


Hello, this is good but doesnt work on service portal


Can you advise how to get that particular example working in service portal ?



Thank you



/Petr


In Service Portal, you just pass the list selector a sys_id or a list of sys_ids that you want displayed.   For example, if the variable name of the list selector is 'selector', you could do something like this:



g_form.setValue('selector', ['sys_id_1', 'sys_id_2', ... , 'sys_id_n']);