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

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

 

hi Brad,

 

this script seems not to be working on portal, although the UI type is selected as ALLsubcategory.PNG

  
can you please help with the script, since below is not working on portal
@Brad Bowman

 

g_form.getControl no longer works in Portal.  I don't see a viable replacement, so a different approach may be necessary.  You can change subcategory to a select box with no choice table or choices, then onChange of Category have a client script call a GlideAjax Script Include that reads the choice table and returns a JSON of the choices.  The advantage to this is that if there are no dependent choices, the client script would recognize/interpret this as setting the variable non-mandatory.  This works in both the native UI and Portal when subcategory is a select box:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	g_form.clearOptions('subcategory');
	var ga = new GlideAjax('ChoiceLookup');
	ga.addParam('sysparm_name', 'getChoices');
	ga.addParam('sysparm_table', 'incident');
	ga.addParam('sysparm_dependent', newValue);
	ga.addParam('sysparm_element', 'subcategory');
	ga.getXML(setValue);
	
	function setValue(response) {
		var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
		g_form.addOption('subcategory','',' -- None --');
		
		if (answer.length == 0) {
			g_form.setMandatory('subcategory', false);
		} else {
			g_form.setMandatory('subcategory', true);
			for(var i=0;i<answer.length;i++) {
				g_form.addOption('subcategory',answer[i].value,answer[i].label);
			}
		}
	}
}

 The Client callable Script Include:

var ChoiceLookup = Class.create();
ChoiceLookup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getChoices : function(){
		var optionsArray = [];
		var json = new JSON();
		var tablename = this.getParameter('sysparm_table');
		var dependent = this.getParameter('sysparm_dependent');
		var element = this.getParameter('sysparm_element');
		var gr = new GlideRecord('sys_choice');
		gr.addQuery('name', tablename);
		gr.addQuery('dependent_value', dependent);
		gr.addQuery('element', element);
		gr.addQuery('inactive', false);
		gr.query();
		while(gr.next()) {
			optionsArray.push({
				'label' : gr.label.toString(),
				'value' : gr.value.toString()
			});
		}
		return json.encode(optionsArray);
	},

    type: 'ChoiceLookup'
});