Populating a list collector selected box from reference variable

malticefalcon
Giga Contributor

I have tried everything recommended to populate the selected side of a list collector with users. Now I have slimmed this down to just two variables to test. I have a reference variable (user) on the sys_user table to select a user. Below that I have a list collector (list_of_users) also tied to the sys_user table. I have a client script that fires (onChange) when the user is selected in the user variable.

The client script is simple and I pasted it below. the console.log shows the sys_id of the user and it should be showing up in the Selected list of the list collector but nothing. Every post and search I did says this should work but it doesn't, any ideas.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
   
    console.log('newValue.toString(): ' + newValue.toString());
    g_form.setValue('list_of_users', newValue.toString());
}
1 ACCEPTED SOLUTION

Sid_Takali
Kilo Patron
Kilo Patron

Hi @malticefalcon try below code

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    var userSysId = newValue.toString();
    var currentValues = g_form.getValue('list_of_users');

    var valuesArray = currentValues ? currentValues.split(',') : [];
    if (!valuesArray.includes(userSysId)) {
        valuesArray.push(userSysId);
    }
    var newValues = valuesArray.join(',');
    g_form.setValue('list_of_users', newValues);
}

View solution in original post

2 REPLIES 2

Sid_Takali
Kilo Patron
Kilo Patron

Hi @malticefalcon try below code

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    var userSysId = newValue.toString();
    var currentValues = g_form.getValue('list_of_users');

    var valuesArray = currentValues ? currentValues.split(',') : [];
    if (!valuesArray.includes(userSysId)) {
        valuesArray.push(userSysId);
    }
    var newValues = valuesArray.join(',');
    g_form.setValue('list_of_users', newValues);
}

malticefalcon
Giga Contributor

Sid,
Thanks for the reply.

I have to say that this has been an experience trying to understand this code as simple as it is. For anyone else out there that might run into this, the code above works great on the Portal side but doesn't work on the ITIL side which is where I want it implemented. I found no documentation on this in my searches on the web and even ChatGPT couldn't help much. Then again, I wasn't specifically asking about ITIL as I assumed the code would be the same. IT'S NOT!
This is some code that I am using for testing but I am sure you get the idea. The if statement is the key to determining Portal or ITIL and which code to run. Hope this helps someone if you are working on the ITIL side.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    console.log('newValue.toString(): ' + newValue.toString());
    console.log('[some sys_id]');

	// Portal side
    if (window === null) {
        g_form.setValue('list_of_users', '[some sys_id]', '[some user name]');
	// ITIL side
    } else {
        addItemstoList('list_of_users', '[some sys_id]', '[some display value]');
    }
}

function addItemstoList(listCollector, sysid, display_value) {

    var varName = listCollector;

    var leftBucket = gel(varName + '_select_0');
    var rightBucket = gel(varName + '_select_1');

    var selectedOptionsIDs = [];
    rightBucket.options.length = 0;

    if (sysid != '') {
        var myCIArray = sysid.split(',');
        var displayvalue = display_value.split(',');

        for (i = 0; i < myCIArray.length; i++) {
            var option = document.createElement('option');
            option.value = myCIArray[i];
            option.text = displayvalue[i];
            // add the option to the bucket
            rightBucket.add(option);
        }
    }

    // sort the buckets 
    sortSelect(rightBucket);
    moveSelectedOptions(selectedOptionsIDs, leftBucket, rightBucket, '--None--');
}