Set Default value and hide Service Catalog Variable based on condition

Bianca1
Giga Contributor

Follow up from: Add Advanced Reference Qualifier to Variable Set that is dependent on a Catalog Item field

In a Service Catalog(shown below) I am trying set a default value on the Product field when their is only one option from the reference table. I would also like to hide this variable or set it as a read-only field with the default option.

I have already added a reference qualifier(follow up link) to that field which will filter how many options are show. If there is more than one option, I want to do nothing.

find_real_file.png

I have been playing around with Business rules, Client Scripts and UI Policies, but none have been successful. Can anyone point me in the direct of the right script to write? 

Thank you.

1 ACCEPTED SOLUTION

I think it needs to be an onChange client script using GlideAjax to query the server, and if the result set has only one member, then return that member, else, do nothing.

View solution in original post

12 REPLIES 12

Mike Allen
Mega Sage

In default value, you can try to do the reference qualifier query.  Then do something like, if the gs.getRowCount() == 1, the default value is that value, variable.sys_id.  Something like that.

 

Or, you can do a catalog client script that has Ajax, so onChange of whatever field is driving the choice, query the server and see how many results, and if one, return it and set the field.

Hi Mike, 

Thank you for your suggestions. On the first one, when you say:

"Then do something like, if the gs.getRowCount() == 1, the default value is that value, variable.sys_id."

Do you mean to add this if statement in the "Default Value" field?

I think it needs to be an onChange client script using GlideAjax to query the server, and if the result set has only one member, then return that member, else, do nothing.

I am working on creating a OnLoad client script with an GlideAjax query.. however the parseResponse is always returning null. I can't figure out why.. any thoughts?

Catalog Client Script:

function onLoad() {
	var cat_item = g_form.getUniqueValue();
	var ga = new GlideAjax('RequestManagerUtils');
	ga.addParam('sysparam_name', 'getRMProductsLength');
	ga.addParam('sysparam_cat_item', cat_item);
	ga.getXML(parseResponse);	
}

function parseResponse(response) {
	var productLength = response.responseXML.documentElement.getAttribute("answer");
	alert(productLength);
}

Script Include:

var RequestManagerUtils = Class.create();
RequestManagerUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	initialize: function() {
	},
	
	
	// returns how many products are in the variable
	getRMProductsLength: function() {
		var cat_item = this.getParameter('sysparam_cat_item');
		var productList = [];
		
		var gr = new GlideRecord("sc_cat_item");
		gr.addQuery("sys_id", cat_item);
		gr.query();
		
		while (gr.next()) {
			productList.push(gr.getValue('u_products'));
		}
		
		return productList.toString().split(',').length;
		
	},
	
	type: 'RequestManagerUtils'
});