Catalog Client Script setValue using a choice Variables text value

jsummers
Kilo Contributor

Trying to use the Text value from a variables choice, chosen on a service catalog form, in a Catalog Client Script and getting nowhere. The idea is to generate a text box string based on the values the user is filling out on the form that they can view and modify, if needed.

Using an onChange Catalog Client Script where 'subject' is the string to be build out and 'category' and 'system' are the variables on the form they are filling out but 'category' is a choice field and we want to be able to use the Text value on that choice field on the setValue string. getDisplayValue is not working.

  var cat = g_form.getValue('category');

  var system = g_form.getValue('system');

  g_form.setValue("subject", 'Outage: ' + cat + ' for ' + system);

1 ACCEPTED SOLUTION

Replace your code with below:




  var cat = g_form.getControl('category');


var catVal = cat.options[cat.selectedIndex].text;


  var system = g_form.getValue('system');


  g_form.setValue("subject", 'Outage: ' + catVal + ' for ' + system);


View solution in original post

9 REPLIES 9

HV1
Mega Guru

You can try either of these:



1. If your choice field is a reference then:     var val = g_form.getReference('category').name;


2. If it not a reference field:   g_form.getDisplayBox('category').value




Regards,


Hardik Vora


jsummers
Kilo Contributor

So the variable is just a Select Box type, with the Question Choices created right there on the variable.



Tried both, although it seemed like the second was the answer, but I'm not getting any results after this on the client script:


  var cat = g_form.getDisplayBox('category').value;


  var system = g_form.getValue('system');


  g_form.setValue("subject", 'Outage: ' + cat + ' for ' + system);



Nothing is happening to the "subject" field, it is blank now when before the 'cat' value was just showing blank but the "subject" field was being populated with everything else.



Tried adding an alert but the alert does not even pop-up when ran:


  var cat = g_form.getDisplayBox('category').value;


  alert(cat);


  var system = g_form.getValue('system');


  g_form.setValue("subject", 'Outage: ' + cat + ' for ' + system);


This should work:



  var cnt = g_form.getControl('category');


  alert(cnt.options[cnt.selectedIndex].text);


jsummers
Kilo Contributor

Not getting the alert either nor is the setValue working.


  var cat = g_form.getDisplayBox('category').value;


  var cnt = g_form.getControl('category');


  alert(cnt.options[cnt.selectedIndex].text);


  var system = g_form.getValue('system');


  g_form.setValue("subject", 'Outage: ' + cat + ' for ' + system);