How to copy the value in one field to other field on the same form?

rkreddy
Giga Expert

Hi Everyone,

I have fields like Name, Address, Country etc on a form, and I have the same related fields in a section of the same form. I have a Yes/No field on the form, when the user select yes, I need to copy the values in Name, Address, Country to related fields in other section of the same form. Could you please help me on this. Thanks in Advance

Regards,

Rakesh

1 ACCEPTED SOLUTION

Joshwa Antony S
Mega Guru

Hi,

 

Create onChange client script that run when the Yes selected in Yes/No field.

Keep the script like this

//check field name and change

var copiedFrom = ['name', 'address', 'country'];
var copiedTo = ['other_name', 'other_address', 'other_country'];


if (newValue == 'yes') {
    for (var i = 0; i < copiedFrom.length; i++) {
        g_form.setValue(copiedTo[i], g_form.getValue(copiedFrom[i]));
    }
}

 

Regards,

JAS

View solution in original post

8 REPLIES 8

Dhananjay Pawar
Kilo Sage

Hi,

Write onChange client script on yes/no field.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var nameVal=g_form.getValue('name'); //Enter correct field name which is in bold

var adrs=g_form.getValue('address'); //Enter correct field name which is in bold

var countryVal=g_form.getReference('country').name; // I believe country is reference field

 

g_form.setValue('name field',nameVal); //Enter correct field name which is in bold

g_form.setValue('address field',adrs); //Enter correct field name which is in bold

g_form.setValue('country field',countryVal); //Enter correct field name which is in bold
}

asifnoor
Kilo Patron

Write a onChange client script on your checkbox field yes/no and add below code

if(newValue == 'yes') {
  var name = g_form.getValue("name"); //check field name and change
  var address = g_form.getValue("address");//check field name and change
  var country = g_form.getValue("country");//check field name and change
  //set in the other section of the form
  g_form.setValue("other_name",name); //check field name and change
  g_form.setValue("other_address",address);//check field name and change
  g_form.setValue("other_country",country);//check field name and change
}

Mark the comment as a correct answer and also helpful if it helps to solve your problem.

sriram35
Kilo Guru

Hi RK,

 

You need to onChange Client Script for this requirement.

 

Please use below script it will help you

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue == 'Yes') {
var name = g_form.getValue("name");
var add = g_form.getValue("address");
var coun = g_form.getValue("country");

g_form.setValue("section_field_name", name);
g_form.setValue("section_field_address", add);
g_form.setValue("section_field_country", coun);
}

}

 

 

If I have answered your question, please mark my response as correct and helpful.

Thanks,

Sriram

 

Ankur Bawiskar
Tera Patron
Tera Patron

@rkreddy 

there are multiple approaches for this

1) if you want this to happen real-time then use onChange client script on that Yes/No field

OR

2) if you want this not to happen real-time then use before insert/update BR and then copy the fields

Regards
Ankur

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