list collector remove specific value based on change of another variable cat item

levino
Giga Guru

Hi Team

 

please advise  if something is wrong with the below script

 

 

Thanks

Levino

 

 

 

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

    // Adjust the condition and value to be removed as needed
    var valueToRemove = 'specific_value_to_remove';
    var listCollectorName = 'list_collector';

    // Get the List Collector variable
    var listCollector = g_form.getControl(listCollectorName);
    if (listCollector) {
        var options = listCollector.options;
        for (var i = options.length - 1; i >= 0; i--) {
            if (options[i].value === valueToRemove) {
                listCollector.remove(i);
            }
        }
    }
}

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

The value of a List Collector is a comma-separated list of sys_ids of records on the List table, so make sure valueToRemove is a sys_id, not display name, etc.  If you want to remove the value from the right / Selected box, one way to do it is to get the current value, split it into an array, find the position of the valueToRemove, splice the array to remove it, then populate the variable with what's left. 

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

    // Adjust the condition and value to be removed as needed
    var valueToRemove = 'specific_value_to_remove'; //sys_id of record on List referenced table
    var listCollectorName = 'list_collector';

    // Get the List Collector variable
    var listCollector = g_form.getValue(listCollectorName);
    if (listCollector) {
    
    // Split List Collector values into an array
    var listArr = listCollector.split(',');

    // Find array position of valueToRemove
    var index = listArr.indexOf(valueToRemove);

    if (index !== -1) { //valueToRemove was found in array
        listArr.splice(index, 1); //remove array member from this array
    }

    g_form.setValue(listCollectorName, listArr.join(','));
}

If you want to remove the specific value from the left / Available box you can change the filter in an onChange script

https://www.servicenow.com/community/itsm-articles/advanced-filtering-of-list-collector-variables-de...  

View solution in original post

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

The value of a List Collector is a comma-separated list of sys_ids of records on the List table, so make sure valueToRemove is a sys_id, not display name, etc.  If you want to remove the value from the right / Selected box, one way to do it is to get the current value, split it into an array, find the position of the valueToRemove, splice the array to remove it, then populate the variable with what's left. 

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

    // Adjust the condition and value to be removed as needed
    var valueToRemove = 'specific_value_to_remove'; //sys_id of record on List referenced table
    var listCollectorName = 'list_collector';

    // Get the List Collector variable
    var listCollector = g_form.getValue(listCollectorName);
    if (listCollector) {
    
    // Split List Collector values into an array
    var listArr = listCollector.split(',');

    // Find array position of valueToRemove
    var index = listArr.indexOf(valueToRemove);

    if (index !== -1) { //valueToRemove was found in array
        listArr.splice(index, 1); //remove array member from this array
    }

    g_form.setValue(listCollectorName, listArr.join(','));
}

If you want to remove the specific value from the left / Available box you can change the filter in an onChange script

https://www.servicenow.com/community/itsm-articles/advanced-filtering-of-list-collector-variables-de...