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

Hi B,

on the client side please adjust sysparam to sysparm in the two lines

ga.addParam('sysparam_name', 'getRMProductsLength');
ga.addParam('sysparam_cat_item', cat_item);

 

The SI line changes

var cat_item = this.getParameter('sysparm_cat_item');

 

Give one more try

------------------------------Also in the background script editor run this code---------------

 

var productList = [];

var gr = new GlideRecord("sc_cat_item");
gr.addQuery("sys_id", 'sys id of the cat item if you know it');
gr.query();

while (gr.next()) {
productList.push(gr.getValue('u_products'));
}
gs.info('len = ' + productList.length);
gs.info('products = ' + productList.toString());

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

You have:

ga.addParam('sysparam_name', 'getRMProductsLength');

when it should be:

ga.addParam('sysparm_name', 'getRMProductsLength');

Example:

var ga = new GlideAjax('HelloWorld');
ga.addParam('sysparm_name', 'helloWorld');
ga.addParam('sysparm_user_name', "Bob");
ga.getXML(HelloWorldParse);
 
function HelloWorldParse(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  alert(answer); }

I fixed the sysparam to sysparm in the two lines in the client script and the one line in the script include however I am still getting a null alert.

 

@vkachineni I have tried using productList.length to get the length of the list, but it always returns 1. That is why I went with productList.toString().split(',').length

Did you also fix the Scriopt include line? it should match the parameter name you are sending

var cat_item = this.getParameter('sysparm_cat_item');

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Yes, I fixed that one too