List Collector Population

General Add comments
by:

What to do when you want to populate the left and right side of a list collector variable (slushbucket) with the values found in your database? For example you want to update the member list of a group in a catalog item. The list collector should start with the current situation in your database:

• Left side should contain all users, but not the users who are currently a member of the selected group
• Right side should contain all users who are currently a member of the selected group

The catalog client script below is an addition to the article “Move List Collector Options” found on the ServiceNow guru website, which has done the most part of finding a suitable solution for this requirement. The added lines give a solution to the list collector limit of showing only the first 100 found in your database. It will add the group member to the left side and than moves it to the right side. This will prevent the possibility of duplicate entries on the right side or in other words that a user can select a group member on the left side and move it again to the right side.

image001

This is just a simple script to help you! If you need more information, please let me know via comment or send me an email: .img[at].img

2 Responses to “List Collector Population”

  1. René Says:

    Hi Marco,

    that seems to be great. But could you please provide the code in text please. It’s unfortunaly not useful as a picture.

    Thanks
    René

  2. Marco Says:

    Hi René,

    No problem.

    var varName = ‘The name of your list collector variable’;
    var rightSide = gel(varName+’_select_1′);
    var leftSide = gel(varName+’_select_0′);

    //Values currently found in your database
    var ids = [Array of ids that needs to be on the right side, the group members];
    var names = [Array of display values belonging to the above ids];

    //It is possible that some/all group members are not present on the left side because ServiceNow will only show the first 100 users
    var checked;
    //Go through all group members found in your database
    for (var i = 0; i < ids.length; i++){
    checked = false;
    //Go through all users available on the left side
    for (var h = 0; h < leftSide.length; h++){
    //When equal mark the leftside user as selected and the group member as found
    if(ids[i] == leftSide.options[h].value) {
    checked = true;
    leftSide.options[h].selected = true;
    break;
    }
    }
    //When group member is not found add it as an option to the left side and mark it as selected
    //Here is where we need the display value for, we don't want to do an extra server side request
    if (!checked) {
    h++;
    leftSide[h] = new Option(names[i], ids[i]);
    leftSide.options[h].selected = true;
    }
    }
    //Now move the selected users from left to right and sort
    moveOptionAndSort(leftSide, rightSide, '–None–', [], '–None–');

    //Deselect all users now available on the right side to show a clean start
    for (i = 0; i < rightSide.length; i++){
    rightSide.options[i].selected = false;
    }

Leave a Reply