Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Clear Options except 'None'

Shihab Ahmed
Tera Contributor

Hi All,

I am trying to develop a catalog client script for a Service Portal Item.

Below is the code I am using:

--------

var cat = newValue;

  g_form.clearOptions('subcategory');

    var choice = new GlideRecord('sys_choice');

  choice.addQuery('dependent_value', cat);

  choice.query(function(choice){

  while(choice.next()){

  g_form.addOption('subcategory', choice.value, choice.value);

  }

  });

--------

I want to dynamically populate subcategory based on selected category.

And every time, category changes, it should change the list of available subcategory.

g_form.clearOptions('subcategory'); , does the job but it also makes the default   '--None--' un-selectable.

I want to remove all options except default '--None--'.

g_form.getControl().options.length =1;, was doing the same job easily for other catalog items but this doesn't work for Service Catalog.

Can someone, help me to fix this issue.

Thanks

Shihab

1 ACCEPTED SOLUTION

Hi Shibab,



addOption accepts the choiceIndex which you can use to specify the order. This will solve your first issue.



void addOption(fieldName, choiceValue, choiceLabel, [choiceIndex]))


Adds a choice to a choice list field. If the index is not specified, the choice parameter is added to the end of the list.
Optional: Use the index field to specify a particular place in the list.
Parameters:
fieldName - specifies the field.
choiceValue - the value stored in the database.
choiceLabel - the value displayed.
choiceIndex (optional) - order of choice in the list.
Returns:
void

View solution in original post

13 REPLIES 13

this is the best way to do this... it enforces the mandatory UI policies and prevents the submission of a '--none--' selection.

nagarjun2
Tera Contributor

Add below line after clearOptions

g_form.addOption('field name',' ','--None--')

 

 

Thanks

dhinesh2205
Tera Contributor

while(choice.next()){

  g_form.addOption('subcategory',"","--None--"");

  g_form.addOption('subcategory', choice.value, choice.value);

  }

GBall
Tera Contributor

I know this topic is a few years old now, but I think I figured out a better way to do this.  Since I still found this on Google hopefully it can still help others.

Below your  g_form.clearOptions('fieldname');  call, add:

 g_form.addOption('fieldname', '', '-- None --', 0); 

This appears to work exactly the same as the OOB 'none' option, including allowing mandatory field enforcement and is compatible with the  g_form.clearValue('fieldname');  method.