How to make the subcategory non-mandatory if has no dependent choices on category on a catalog item.

Nishant16
Tera Expert

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?subcategory.PNG

 

 

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);
	}
}}

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

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);
	}
}

 

View solution in original post

9 REPLIES 9

Hi Brad,

 

Unfortunately this has not been working for me and getting the below error: There is a JavaScript error in your browser console

Nishant16_0-1666868474876.png

 

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?

i am getting below error, and cant seem to understand why this is happening:

Nishant16_0-1666881705571.png

 

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.

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.