The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How can i copy the contents of one field and duplicate it on another field?

thiraj
Tera Contributor

Hi Team,

I want to copy the contents from one field (String1) and duplicate it on another field (String2) as the default value. Users with access to edit can edit the (String2) field. But by default, (String1) should be the same as (String2).

String1 - u_os_host_name

String2 - u_inventory_name

find_real_file.png

They are both in the same table / form.

All help is appreciated.


Thank you

Regards

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi,



You can do this a couple ways. The first is with an onChange client script to set the value if none already exists. For example:



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


  if (newValue === '') {


      return;


  }


  if (g_form.getValue('u_field2') != '') {


        return; // a value already exists. do not overwrite


  }



  // Copy the value from u_field1 to u_field2


  g_form.setValue('u_field2', g_form.getValue('u_field1'));


}


View solution in original post

10 REPLIES 10

Hi,



Sorry for the confusion. An onChange client script will do exactly what you mentioned in your requirements. As soon as someone changes u_os_host_name (and hits tab or cursors to another field) the u_inventory_name field will be populated as long as nothing else is there. Can you share with me all the details of your client script per the script I gave you? It could be another field is not set.



Screenshots are most appreciated. Thank you


Hi Chuck,



The Initial Tab works fine. But if i change the (Field1) name again, it does not change (Field2) the second time.


The first screen is the initial input which works perfect.


find_real_file.png


The below screen is the second input when i changed the (Fiels1 - OS Host Name), but Inventory name did not change.



find_real_file.png


I want the inventory name to change with the host name. BUT...If the inventory name is changed by someone who has permission, then it should show the new value regardless of the default value.


Thank you.


HI,



Thank you for the clarification. My script simply checks "only update the Inventory Name field if it is blank." If there is a value (either set by a previous run of the client script, or by a person) it leaves it alone.



Unfortunately, a client script cannot tell the difference whether a new value was entered in Inventory Name by the script or by a person. That's not something ServiceNow or JavaScript has visibility to or control over.


Hi Chuck,



Thank you for you quick responses.


The above answer you mentioned is correct. The requirements given to me were a bit misleading. Hence i thought it was to change all the time. But the answer you initially provided is the correct answer and fulfills the requirements perfectly.



Thank you


I'm glad you got it clarified and the solution I provided worked for you. Thank you for using the community.