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

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

thank you!  It worked when making sure oldValue is properly spelled as mentioned below.

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

tami4
Tera Contributor

Thanks so much! 

tami4
Tera Contributor

Business case:

I have a field on a catalog item, which user fills what they want a new Service to be named.

In another part of the form, the same Service name is needed when the fulfiller is creating Server/Cluster (as the Owning service). 

Of course I could reference there "Make it the same as xx field" but was trying to populate it the same by script.

I am a catalog editor but not a sys admin, so I will inquire within to find an answer.

Thanks for your help; but even the final context did not work for me.