Catalogue Client Script on List Collector Variable

akchauhan
Tera Contributor

I have created a List collector Variable in Service Catalogue and created their 3 choices ( A , B , C) and get it referenced through question_choice table. Now the requirement is that when I select the B choice then I should not get the option to select C option and vice versa. But for Option A , I can select all 3 choice.

Please assist.

1 ACCEPTED SOLUTION

Ravi Gaurav
Giga Sage
Giga Sage

Hi @akchauhan 
I have tried in PDI . Its working as expected . PFA code and screenshot for your reference.

 

RaviGaurav_0-1735895410387.png

 

RaviGaurav_1-1735895515668.png

 

Note:- I have used hardcoded sys_id of choices.

Explanation:

  1. Selection Rules:
    • If B and C are both selected together, regardless of whether A is selected, the script triggers an alert and clears the List Collector value.
  2. Behavior:
    • Users can select any single option or combinations like A + B or A + C.
    • Users cannot select B and C together, even if A is included.

Testing:

  1. Test Cases:
    • Select B and C → Alert should appear, and the value should clear.
    • Select A and B or A and C → Selection should be allowed.
    • Select A, B, and C → Alert should appear, and the value should clear.
    • Select A only → Selection should be allowed.

Code :-

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

    var valueSelected = g_form.getValue('test_list_collector');


    // Sys IDs for the choices
    var sysIdA = '5798dc7283fe16108090fcc6feaad372'; // A
    var sysIdB = '3be8d0b283fe16108090fcc6feaad36a'; // B
    var sysIdC = '5df8dc7283fe16108090fcc6feaad313'; // C

    // Check if A, B, and C are selected
    var isASelected = valueSelected.indexOf(sysIdA) > -1;
    var isBSelected = valueSelected.indexOf(sysIdB) > -1;
    var isCSelected = valueSelected.indexOf(sysIdC) > -1;

    // Logic to enforce the selection rules
    if (isBSelected && isCSelected) {
        alert('You cannot select both option B and option C together.');
        g_form.clearValue('test_list_collector'); // Clear the List Collector value
    }


}

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

7 REPLIES 7

@akchauhan 

Hope you are doing good.

Did my reply answer your question?

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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

Ravi Gaurav
Giga Sage
Giga Sage

Hi @akchauhan 
I have tried in PDI . Its working as expected . PFA code and screenshot for your reference.

 

RaviGaurav_0-1735895410387.png

 

RaviGaurav_1-1735895515668.png

 

Note:- I have used hardcoded sys_id of choices.

Explanation:

  1. Selection Rules:
    • If B and C are both selected together, regardless of whether A is selected, the script triggers an alert and clears the List Collector value.
  2. Behavior:
    • Users can select any single option or combinations like A + B or A + C.
    • Users cannot select B and C together, even if A is included.

Testing:

  1. Test Cases:
    • Select B and C → Alert should appear, and the value should clear.
    • Select A and B or A and C → Selection should be allowed.
    • Select A, B, and C → Alert should appear, and the value should clear.
    • Select A only → Selection should be allowed.

Code :-

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

    var valueSelected = g_form.getValue('test_list_collector');


    // Sys IDs for the choices
    var sysIdA = '5798dc7283fe16108090fcc6feaad372'; // A
    var sysIdB = '3be8d0b283fe16108090fcc6feaad36a'; // B
    var sysIdC = '5df8dc7283fe16108090fcc6feaad313'; // C

    // Check if A, B, and C are selected
    var isASelected = valueSelected.indexOf(sysIdA) > -1;
    var isBSelected = valueSelected.indexOf(sysIdB) > -1;
    var isCSelected = valueSelected.indexOf(sysIdC) > -1;

    // Logic to enforce the selection rules
    if (isBSelected && isCSelected) {
        alert('You cannot select both option B and option C together.');
        g_form.clearValue('test_list_collector'); // Clear the List Collector value
    }


}

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Thank you ravi. I have been struggling with this requirement for quite some time. Your code really helped.