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

Hi Shihab,


We are also facing same issue with multiple Catalog items. Can you please provide some insights into UI Policy approach.


Thanks in advance.



Regards,


Mahesh.


shloke04
Kilo Patron

Hi,



Using Glide Record on the client side is not a good practice, instead I would suggest you to create a custom table at the backend say "XYZ" table having fields Category and Subcategory.


You can define the choices you need on your catalog form in the Category and Subcategory field in your Custom table itself.



Now in order to display those choice Values Create Two variables in your Catalog Form as mentioned Below:



1) Category-


Type as Lookup Select Box



find_real_file.png



Here in the Above screen shot you need to select the Lookup Table as your Custom Table and Lookup field as "Category" defined in your custom table.



2) Define another Variable as Subcategory with Type as "Lookup Select Box" only and configure as mentioned below:



Select the Table as your Custom Table Name and Lookup Value field as "Subcategory" from the Custom Table and give the Reference Qualifier as mentioned below:



javascript: 'u_category='+current.variables.category;



Where "u_category is the Category field you defined in the Custom table and "category" is the variable name for Category on your catalog item.



In the Subcategory Variable you need to define the Variable Attribute available under Default Tab as mentioned below:


ref_qual_elements=category



Screen shots below for the same configuration:



find_real_file.png



find_real_file.png



Hope this helps. Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

jamieliao
Tera Contributor

It would be nice to give an option to clear all except none in the future, am sure many developers would appreciate this


Jace Benson
Mega Sage

Another way to do this is to set up dependent fields/variables;

https://blog.jacebenson.com/post/2017-10-28-lookup-select-attributes/

jacob_hays00
Giga Contributor

I agree, it would be nice if the clear option did not clear the --none-- value. One way around it, I believe, is to re-add the "-- None --" as an option. 

Example:

g_form.addOption('<fieldname>', '' , '-- None --');

Make sure to set the choiceValue parameter as '' to prevent it from assigning it is an actual choice value.

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