- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 10:53 AM - edited 10-11-2022 10:59 AM
I have Category and subcategory fields on a Catalog item, both mandatory. Now i want to make the subcategory non-mandatory if it has no dependent choices.
Can someone help me How to achieve this?
In one of the posts i found the below on change client script on Category which is not working for me:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue !='') {
var field = g_form.getControl('subcategory');
var options = field.options.length;
if(options==0){
g_form.setMandatory(field, false);
}
}}
Solved! Go to Solution.
- Labels:
-
Request Management
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 12:48 PM
If you alert on options you'll see that the onChange script is running before the ref_qual_elements / reference qualifier finishes, so it contains the number of subcategories for the previous category. I was also seeing 1, not 0, when -- None -- was the only choice, so here's a script that works to wait a bit then set Mandatory or not:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
setTimeout(function() {
setMand();
}, 1500);
}
function setMand() {
var field = g_form.getControl('subcategory');
var options = field.options.length;
if (options==1) {
g_form.setMandatory('subcategory', false);
} else {
g_form.setMandatory('subcategory', true);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2022 04:01 AM
Hi Brad,
Unfortunately this has not been working for me and getting the below error: There is a JavaScript error in your browser console
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2022 07:12 AM - edited 10-27-2022 07:13 AM
I'm not getting this. It is usually caused by the Script Include containing an error, or using getXMLWait instead of getXML. If you open your browser developer tools and clear the console log before changing Category, what is the error shown? Does it happen if you choose a Category that does have Subcategories, doesn't have any, or both?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2022 07:41 AM
i am getting below error, and cant seem to understand why this is happening:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2022 12:35 PM
i have tried but still no luck, i have tried for both when category has related sub categories and no subcategories.
getting the same error when i am using sys_choice table in the script include.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2022 09:43 AM
That error means the Script Include is not found or contains a syntax or logic error that is preventing it from returning anything. Make sure the Client callable box is checked, and that the Client Script and Script Include are as I posted. Also try running this in the native UI and adding gs.addInfoMessage or gs.info logs to the Script Include to confirm that it is running, and to see how far it is getting.