Catalog client script to remove drop down value if the values starts with SNOW

BanuMahalakshmi
Tera Contributor

Hi,

 

Need your help to remove(hide) the values starts with SNOW from the category drop down list into SP. The below script is working correctly  but i should remove dynamically instead of calling removeoption for each category.  If a new value(starts with SNOW) category is added in future, this code should remove it automatically from the particular record producer.

 

function onLoad() {
//Type appropriate comment here, and begin script below

g_form.removeOption('category', 'SNOW-Modules');
g_form.removeOption('category', 'SNOW-Compensation');

}

1 ACCEPTED SOLUTION

Phanindra N
Tera Guru

Instead of going with catalog client, can you try using reference qualifier to not show options starting with SNOW.

View solution in original post

13 REPLIES 13

@BanuMahalakshmi 

 

Use index of

 

newValue.indexOf()

Karthiga S
Kilo Sage

Hi @BanuMahalakshmi 

You can try this script with Onload also.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var selectBox = g_form.getControl('your_dropdown_field');
for (var i = 0; i < selectBox.options.length; i++) {
if (selectBox.options[i].text.startsWith('SNOW')) {
selectBox.remove(i);
}
}
}

 

Please mark it Correct and Hit Like if you find this helpful!

 

Regards,

Karthiga

SwarnadeepNandy
Mega Sage

Hello @BanuMahalakshmi 

 

If you put all categories like this, obviously it wouldn't be scalable.

 

  • One way to achieve this is to use a client script that loops through the options and removes the ones that match a certain condition. Try this below code.

 

function onLoad() {
    //Get the choice list element 
    var category = g_form.getControl(‘category’);

    //Loop through the options 
    for (var i = 0; i < category.options.length; i++) {

        //Get the option value
        var option = category.options[i].value;

        //Check if it starts with SNOW 
        if (option.startsWith(‘SNOW’)) {

            //Remove the option 
            g_form.removeOption(‘category’, option);
        }
    }
}​

 

  • Another way to achieve this is to use a UI policy that sets the available choices for the category field based on a condition. 
    • Condition: true (always apply)
    • Script: true (use script to set choices)
    • Script: 

 

 

//Get the choice list element 
var category = g_form.getControl(‘category’);

//Create an array of choices to keep 
var choices = [];

//Loop through the options 
for (var i = 0; i < category.options.length; i++) {

    //Get the option value 
    var option = category.options[i].value;

    //Check if it does not start with SNOW 
    if (!option.startsWith(‘SNOW’)) {

        //Add the option to the array 
        choices.push(option);
    }
}
//Set the available choices for the category field 
g_form.setAvailableChoices(‘category’, choices);

 

 

 

Hope this helps

 

Kind Regards,

Swarnadeep Nandy

Pavankumar_1
Mega Patron

Hi @BanuMahalakshmi ,

you can try below onchange client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	if(newValue.indexOf("SNOW")==0){//choice should starts with snow
		g_form.removeOption('category',newValue);
	}
}

or on submit

function onSubmit() {
	var val=g_form.getValue('category');
	if(val.indexOf("SNOW")==0){
		return false;
	}
}
If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi Thanks for reply,  The newValue can get only one value from the category drop down list  but i want to check all the drop down values. Also the script g_form.getValue('category') also fetching only one value but not all the values from the category drop down list.