One option to be selected from List collector

PK14
Kilo Guru

Hi ServiceNow Expert,

 

I have a requirement on list collector field, there are N number of option (Read, write and execute) that can be selected.

For example : There are three options: Codw read, codw write and codw execute. When user selects codw read, codw write and codw execute should disappear or hidden. Same works when codw write is selected (hide codw read and  codw execute)
Please assist me on this. 

 

below code written but doesn't work: 

 

 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 
   function onChangeListCollectorField() {
    var codwRead = false;
    var codwWrite = false;
    var codwExecute = false;
    var gswRead = false;
    var gswWrite = false;
    var gswExecute = false;
    // Assuming you have a list collector field with the ID 'list_collector_field'
    var selectedOptions = g_form.getValue('access_level');
    // Loop through the selected options to check for violations
    for (var i = 0; i < selectedOptions.length; i++) {
        var option = selectedOptions[i];
        if (option.indexOf('CODW') !== -1) {
            if (option === 'CODW - Read') {
                codwRead = true;
                if (codwWrite || codwExecute) {
                    g_form.addErrorMessage('Cannot select codw - read along with codw - write or codw - execute.');
                    g_form.clearValue('access_level');
                    return;
                }
            } else if (option === 'CODW - Write') {
                codwWrite = true;
                if (codwRead || codwExecute) {
                    g_form.addErrorMessage('Cannot select codw - write along with codw - read or codw - execute.');
                    g_form.clearValue('access_level');
                    return;
                }
            } else if (option === 'CODW - Execute') {
                codwExecute = true;
                if (codwRead || codwWrite) {
                    g_form.addErrorMessage('Cannot select codw - execute along with codw - read or codw - write.');
                    g_form.clearValue('access_level');
                    return;
                }
            }
        } else if (option.indexOf('GSW') !== -1) {
            if (option === 'GSW - Read') {
                gswRead = true;
                if (gswWrite || gswExecute) {
                    g_form.addErrorMessage('Cannot select gsw - read along with gsw - write or gsw - execute.');
                    g_form.clearValue('access_level');
                    return;
                }
            } else if (option === 'GSW - Write') {
                gswWrite = true;
                if (gswRead || gswExecute) {
                    g_form.addErrorMessage('Cannot select gsw - write along with gsw - read or gsw - execute.');
                    g_form.clearValue('access_level');
                    return;
                }
            } else if (option === 'GSW - Execute') {
                gswExecute = true;
                if (gswRead || gswWrite) {
                    g_form.addErrorMessage('Cannot select gsw - execute along with gsw - read or gsw - write.');
                    g_form.clearValue('access_level');
                    return;
                }
            }
        }
    }
}
}
 
 
 
 
 
 
 
 
 
 
 
1 REPLY 1

Vishal Birajdar
Giga Sage

Hello PK14 ,

 

Can you please let me know which table List collector is referencing ?

 

In my case , Just for testing I'm referencing it to "Question choice" table and I'm able to achieve this.

 

Step 1  : Create Catalog Item Variable 

Name : Access

Type : List Collector

List Table : Question Choice

Reference Qualifier : 

javascript&colon;
if (current.variables.access) {
        /* If any value selected in List collector then don't show other values*/
       "question=2eb25ddac3897110ab85241ce0013134^value=" + current.variables.access;

} else {
      * If List collector is empty or removed selected value from list then show all values*/
       "question=2eb25ddac3897110ab85241ce0013134";  // sys_id of "Access" variable
}

 

VishalBirajdar7_0-1693898513704.png

 

 

Step 2 : Create 3 question choices in Question choice table as per scrrenshot

1.Read

2.Write

3.execute 

 

VishalBirajdar7_1-1693898555146.png

 

 

Output : 

 

Test case 1 : List collector is empty i.e no value selected then show all Read,write,execute

 

VishalBirajdar7_2-1693898651305.png

 

Test case 2 : If you select "Read" then "Write" and "Execute" will not display

 

VishalBirajdar7_3-1693898709638.png

 

Same for other

 

VishalBirajdar7_4-1693898746660.png

 

You can use same logic for your case ....!!

Hope this will help you..!!

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates