- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2017 07:04 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2017 11:17 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2020 09:45 AM
this is the best way to do this... it enforces the mandatory UI policies and prevents the submission of a '--none--' selection.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2020 06:00 AM
Add below line after clearOptions
g_form.addOption('field name',' ','--None--')
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2020 10:41 AM
while(choice.next()){
g_form.addOption('subcategory',"","--None--"");
g_form.addOption('subcategory', choice.value, choice.value);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2021 12:31 PM
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.