How to show/hide choices for list collector variables

Koki Sugimori
Tera Contributor

We are currently creating two variables (a select box variable and a list collector variable) in the catalog item.

 

As an example, we have created choices A, B, C, D, and E for the select box variable and choices 1, 2, 3, 4, and 5 for the list collector.

 

【Set values for the list collector】
 List table: Question Choice[question_choice].
 Reference qualifier: sys_created_onON2024-12-25@javascript:gs.dateGenerate('2024-12-25','start')@javascript:gs.dateGenerate('2024-12 -25','end')

 

【Question】
I would like the list collector variable to show 1, 2, and 3 (4 and 5 are hidden) when A, B, and C are selected in the select box variable, while 4 and 5 are shown (1, 2, and 3 are hidden) when D and E are selected.

 

This is indicated in the image diagram.

KokiSugimori_0-1736316638084.png

 

Can anyone assist me on how to implement the above?
Thank you in advance.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Koki Sugimori 

you can apply advanced ref qualifier on list collector and make it dependent on select box

Something like this

Ensure you give the correct variable name and correct sysIds for the question choices from question_choice table

javascript: var val = current.variables.selectBoxVariableName; var query; if(val == 'A' || val == 'B' || val == 'C') query = 'sysIDINChoice1SysId,Choice2SysId,Choice3SysId'; else query = 'sys_idINChoice4SysId,Choice5SysId'; query;

In variable attributes please give this

ref_qual_elements=selectBoxVariableName

 

AnkurBawiskar_0-1736402248663.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Kalyani Jangam1
Mega Sage
Mega Sage

Hi @Koki Sugimori 
You can use this code in OnChange client script

if (g_form.getValue(choice1(A value) || choice1(B Value) || choice3(C Value)'){

g_form.addOption('list_collector_field_value','1');

g_form.addOption('list_collector_field_value','2');

g_form.addOption('list_collector_field_value','3');

g_form.removeOption('list_collector_field_value','4');

g_form.removeOption('list_collector_field_value','5');

}

else if(g_form.getValue(choice3(D value) || choice4(E Value)){

g_form.removeOption('list_collector_field_value','1');

g_form.removeOption('list_collector_field_value','2');

g_form.removeOption('list_collector_field_value','3');

g_form.addOption('list_collector_field_value','4');

g_form.addOption('list_collector_field_value','5');

}
Please check and let me know if it helpful or not.

PraveenK1149237
Tera Expert
function onChange(control, oldValue, newValue, isLoading) {
    var filter = '';
    if (['A', 'B', 'C'].includes(newValue)) {
        filter = 'c3d35d82c37122005871d44d81d3ae91,13ec9310c3f9521012fcdf0d05013156,0a232a0013691200042ab3173244b075';
    } else if (['D', 'E'].includes(newValue)) {
        filter = 'd26c90e9c0892300964f80d1691e1a37,8368dc69c0892300964f80d1691e1a76';
    }
    else if(newValue == "")
    {
        filter = "";    
    }
    g_form.setValue('list_collector', filter);
}
This is the on change client script(on select box variable), change the "filter" as per your requirement as I used sys_user table for list collector. If you are not using none in select box remove the 3rd Else If 
PraveenK1149237_0-1736320472032.pngPraveenK1149237_1-1736320498680.pngPraveenK1149237_2-1736320517353.png

Mark my answer helpful if this helps you.

Dear @PraveenK1149237, Thank you.

 

My explanation was insufficient. My apologies.

The solution you answered (using the catalog client script onChange) automatically sets the choices in the list collector field, but this time I want to show or hide the list of choices that can be selected in the list collector.

 

Can you please provide new support for the above?
Thank you in advance.

 

Can you tell me is there any relation between the select box values and the values that appear on the list collector.