Catalog Client script - how to copy user input into a single text field, to another field

tami4
Tera Contributor

I did some research and thought I could accomplish what I need, but it doesn't work.

I would like to take some free-text input from a variable, and copy it to another variable field (which I would set as read-only via UI policy).

Is that possible? Let me add that I'm fairly new at writing these.
Below is my script which doesn't work like I need it to. 

Catalog Client script onChange for the variable that user will fill "fill_this".
If he changes his mind / populates a new value into the first field, the 2nd field will clear and re-populate with his new text.

My 2 variables are
fill_this (where user enters text)
populate_that (where I also want to auto-populate the same text)

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == "") {

return;

 }

 if(newValue != oldvalue) {

var data = g_form.getDisplayValue('fill_this');


g_form.setValue('populate_that', data); 


}

}

find_real_file.png

 

And if I may add another scenario (different use-case) I would like to be able to do the same with a reference table.
User selects a service from a Reference variable; I want the same service to populate in another variable field on the form (read-only field per ui policy).  If he changes his mind, the 2nd value is also cleared and re-populated.

2 ACCEPTED SOLUTIONS

Hi,

What is the business requirement here as both the variables will hold same value? is this valid business requirement

if both are string type then below should work

the below would be onchange catalog client script on the variable fill_this

since the 2nd variable would be auto-populated set it read-only in same script

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading) {
g_form.setReadOnly('populate_that', true); // set to read-only on load
return;
}

// script to set value to 2nd variable

if(newValue == "")
g_form.clearValue('populate_that');

if(newValue != oldvalue) {
g_form.setValue('populate_that', newValue); 
}

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

Hi All. Mike's solution worked for me. What I noticed is in Tami's script, make sure to capitalize the V in "oldValue".

View solution in original post

9 REPLIES 9

Mike Patel
Tera Sage

change

var data = g_form.getDisplayValue('fill_this');

to

var data = g_form.getValue('fill_this');

tami4
Tera Contributor

This did not work for me.

The 2nd field remains blank.

Any other ideas? 

You will have to turn of UI policy that set read only and use client script to do set it to read only.

Create onload client script

with g_form.setReadOnly('fill_this', true);

Onchange script will work if fill_this variable is string field.

var data = g_form.getValue('fill_this');

tami4
Tera Contributor

This also doesn't work for me.

Let me say, having the 2nd field read-only is only for illustration in my screenshot (purpose is to keep someone from changing it). I don't actually have that UI Policy set just yet.