How to set a value on a single text field or select box field, after selecting a choice from another variable?

alberttagle
Tera Contributor

Hi,

I'm trying to script, so that when I choose an option in two different select box variables (Environment and Version), the "target server" variable will have a specific text (for single line text), or maybe set the choice if I use select box.

I have done the following so far:

find_real_file.png

find_real_file.png

 

 

1 ACCEPTED SOLUTION

Double check your using the correct field names in your getters and setters. Also add some alerts throughout the code to see where the script is breaking.

I have a feeling this could be the condition which triggers the client script. You want to change the variable name to either environment or version. This way the client script is triggered when one of those is changed, not onChange of the target server field.

View solution in original post

9 REPLIES 9

Hi, Dylan.

After I made the complete script, I found that it is only triggering the script when I change the choice selected in what is in the variable name.

What I mean is I have a choice list under Environment (a, b, c, and d). and another set of choices under Version (a,b, c, and d).

I used 'version' for variable name in the onchange script.

When I tested out the record producer, I select from the Environment, and after I select a choice in the Version, the targetserver field results just fine.  but if I change my mind and selected a different environment, if wouldn't change the result in the target server - I had to select a different option in the version to take effect.

Is there any additional script I can add to automatically change the targetserver result as soon as I change either the Version or the Environment?

Thanks!

Unfortunately, you can't select multiple fields to trigger a client script, but if anyone else knows of a way to do so, feel free to let us know. 

The simplest solution is to create another catalog client script and paste in the same script just triggered onChange of the environment field. Kinda stinks you have to copy and paste code like this, but it's something you just have to do and get's the job done.

The workaround works!!!

I know ServiceNow lacks something here and there, and perhaps could be taken as future enhancement, but I really don't mind having to copy and paste the codes, so long as they work.

I wish there's a compilation of "tricks"/information like this online, so anyone as green as me can look up as FAQs.

Thanks again, Dylan! 

Big thanks @Dylan, i had similar issue and it did helped me very well, really appreciate your effort. 🙂

Jaya4
Kilo Guru

Hi

 

You can try addOption/clearOption function here.

Once the end use change the environment and version, you can change the option in target server.

Below is the example :

 

var dbenvironment  = g_form.getValue("db_enviornment");
var dbversion = g_form.getValue("dbversion");
g_form.clearOptions("targetserver");

if(dbenvironment == 'Development' and dbversion == 'SQL Server 2014'){
g_form.addOption(String fieldName, String choiceValue, String choiceLabel)
        g_form.addOption("targetserver","DevA","A");
        g_form.addOption("targetserver","DevB","B");
        
    }
     if(dbenvironment == 'Test' and dbversion == 'SQL Server 2014'){
        g_form.addOption("targetserver","testB","B");
     }
    ifif(dbenvironment == 'Prod' and dbversion == 'SQL Server 2014'){
        g_form.addOption("targetserver","prodA","A");}

 

Let me know if it helps

 

-Jaya