Remove selected value from list collector based on selected value in current list collector

Edwin Fuller
Tera Guru

I have a list collector that should only allow either "Dual Monitor" or "Single Monitor" to be selected, but never both. This list collector has other values that can be selected that should not be removed when removing "Dual Monitor" or "Single Monitor". 

 

Example:

I add Dual Monitor to list collector and then add Single Monitor to list collector. Script should remove Dual Monitor and keep Single Monitor.

 

OR

 

I add Single Monitor to list collector and then add Dual Monitor to list collector. Script should remove Single Monitor and keep Dual Monitor.

 

In both examples, if other values are part of the list collector then they should remain

EdwinFuller_0-1681878060685.png

 

6 REPLIES 6

Aman Kumar S
Kilo Patron

Hi @Edwin Fuller ,

 

Try below client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ulist2 = newValue;
    var newList = [];
    if (ulist2.indexOf("sys-id-of-sm-device") > -1 && ulist2.indexOf("sys-id-of-dm-device") > -1) {
        newList = ulist2.split(',');
        var indexOfDM = newList.indexOf("sys-id-of-dm-device");
        var indexOfSM = newList.indexOf("sys-id-of-sm-device");
        if (indexOfDM > indexOfSM) {
            var newList1 = newList.splice(indexOfSM, 1);
            g_form.setValue("users_list", newList);
        } else {
            var newList2 = newList.splice(indexOfDM, 1);
            g_form.setValue("users_list", newList);
        }
    }
}

 

Let me know how it goes.

 

Best Regards
Aman Kumar

@Aman Kumar S I used this approach for a similar ask and it worked successfully thank you!

Small note for anyone using in the future - this won't work if you want to use it through Virtual Agent - as g_form.getControl isnt supported.