The CreatorCon Call for Content is officially open! Get started here.

How to achieve the list collector values to clear the values of its reference

Exp1Gr
Giga Contributor

Hi can one guide me or Implemented below requirment

I have a catalog item with two list collector variables. The second variable should display values based on the selections made in the first variable. When multiple options are selected in the first variable, the corresponding reference values are available in the second variable for selection.

I wrote an onChange client script to clear values in the second variable when a choice is deselected from the first variable. However, the current script clears all values in the second variable. The requirement is to clear only the values that belong to the deselected choice, while keeping the others intact.

Looking for insights or suggestions on how to adjust the script to achieve this.

GunduSrikan_0-1755848394207.png

 

Thanks in advance!

9 REPLIES 9

function onSubmit() {
if (g_scratchpad.isABCDFormValid) {
return true;
}

var processes = g_form.getValue('u_process');
var modules = g_form.getValue('u_function');

var ajax = new GlideAjax('ABCDServiceCatalogUtils');
ajax.addParam('sysparm_name', 'hasValidModules');
ajax.addParam('sysparm_processes', processes);
ajax.addParam('sysparm_modules', modules);

ajax.getXMLAnswer(function(answer) {
if (answer === 'false') {
g_form.addErrorMessage('Text.');
} else {
g_scratchpad.isDTCCFormValid = true;
}
});

g_form.submit(g_form.getActionName());
return false;
}

@Exp1Gr 

please don't use onSubmit but please use onChange catalog client script on 1st variable

I already suggested approach below.

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

Hello @Exp1Gr ,

Instead of creating onSubmit client script, create an onChange client script on 1st variable (process). You don't required to create saperate client script to remove values from 2nd variable. 

onChange catalog client script for your reference:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || oldValue === newValue) {

        return;

    }
    var processes = newValue;

    // Make the GlideAjax call to your Script Include
    var ga = new GlideAjax('ABCDServiceCatalogUtils');
    ga.addParam('sysparm_name', 'hasValidModules');
    ga.addParam('sysparm_processes', processes);

    ga.getXMLAnswer(function(response) {
        var ans = response;
        var funcRecords= JSON.parse(ans);

        if (funcRecords.length > 0) {
            var result = funcRecords.join(', ');
            g_form.setValue('u_function', result);
        } else {
            g_form.setValue('u_function', '');
        }
    });
}

 

Please mark my response as Correct and Helpful if it assists you.


Thanks,
Ashish Parab

Ankur Bawiskar
Tera Patron
Tera Patron

@Exp1Gr 

do this

1) write onChange catalog client script on 1st variable and pass all the values to script include function

2) that script include function will return matching records for 2nd variable, clear the 2nd variable and then set the values which you got from Ajax

With this approach you need not worry on removing only the values that belong to deselected choice etc

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

@Exp1Gr 

Hope you are doing good.

Did my reply answer your question?

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