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

Omkar Mone
Mega Sage

Hello,

 

See if this below script works.

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

    var parm = (window === null) ? 'portal' : 'native';
    var varName = 'your_list_var_name';
    var filterString = '';

    var choiceA = 'choice_a_sys_id';
    var choiceB = 'choice_b_sys_id';
    var choiceC = 'choice_c_sys_id';

    if (newValue.indexOf('Choice A') != -1) {
        filterString = 'sys_idIN' + choiceA + ',' + choiceB + ',' + choiceC;
    } else if (newValue.indexOf('Choice B') != -1) {
        filterString = 'sys_idIN' + choiceB;
    } else if (newValue.indexOf('Choice C') != -1) {
        filterString = 'sys_idIN' + choiceC;
    }

    if (parm == 'portal') {
        var myListCollector = g_list.get(varName);
        myListCollector.reset();
        myListCollector.setQuery(filterString);
    } else {
        window[varName + 'g_filter'].reset();
        window[varName + 'g_filter'].setQuery(filterString);
        window[varName + 'acRequest'](null);
    }
}

 

Ankur Bawiskar
Tera Patron
Tera Patron

@akchauhan 

what happens if option B or C is selected in combination with A?

Is this an actual business requirement?

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

Dear Ankur,

many thanks for your response.

With A, either B or C can be selected but B&C cannot be selected at one go either with A or without A.

@akchauhan 

simply use onSubmit catalog client script

function onSubmit() {

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

    if (valueSelected.indexOf('choiceBSysId') > -1 && valueSelected.indexOf('choiceCSysId') > -1) {
        g_form.addErrorMessage('Please select either option B or option C');
        return false;
    }

}

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