Clear default value from list collector on selection

Stermieri
Tera Contributor

On a form I'm using a list collector that has "All" as the default value.
How can I clear "All" as soon as any other selection is made?

 

Stermieri_1-1693580664255.png

 

I was trying to use an onchange catalog client script to remove the option but this isn't working:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue == 'Alaska') {
        g_form.removeOption('work_state', 'all', '- All -');
    }
}

Any suggestions?

1 ACCEPTED SOLUTION

Craig Gruwell
Mega Sage

Hi Stermieri,

 

Try this:

 

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

    var defaultValue = "<sys_id of the default value>";
    var arrList = g_form.getValue('work_state').split(",");

    //Is 'All' is in list, get its' index....
    var all_index = -1;
    for (i = 0; i < arrList.length; i++) {
        if (arrList[i] == defaultValue) {
            all_index = i;
        }
    }

    if (all_index > -1) {

        //if 'All' was the default value (and there is more than 1 item in field), remove 'All'
        if (all_index == 0 && arrList.length > 1) {
            arrList.splice(all_index, 1);
            g_form.setValue('work_state', arrList.join(","));
        } else {
            //If 'All' added later, remove everything else
            g_form.setValue('work_state', defaultValue);
        }

    }

}

 

View solution in original post

3 REPLIES 3

Craig Gruwell
Mega Sage

Hi Stermieri,

 

Try this:

 

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

    var defaultValue = "<sys_id of the default value>";
    var arrList = g_form.getValue('work_state').split(",");

    //Is 'All' is in list, get its' index....
    var all_index = -1;
    for (i = 0; i < arrList.length; i++) {
        if (arrList[i] == defaultValue) {
            all_index = i;
        }
    }

    if (all_index > -1) {

        //if 'All' was the default value (and there is more than 1 item in field), remove 'All'
        if (all_index == 0 && arrList.length > 1) {
            arrList.splice(all_index, 1);
            g_form.setValue('work_state', arrList.join(","));
        } else {
            //If 'All' added later, remove everything else
            g_form.setValue('work_state', defaultValue);
        }

    }

}

 

Hi Craig,

I have a similar requirement where I have to remove email ids from Users field on Scheduled Reports table which does not belong to particular regex (/^[a-zA-Z0-9._-]+@(example)\.com$/), like if user is trying to add (abc@abc.com) it should get removed from the list.

Stermieri
Tera Contributor

Thank you Craig,

That works beautifully