- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 03:45 PM
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
They are both in the same table / form.
All help is appreciated.
Thank you
Regards
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 03:51 PM
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'));
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 04:58 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 05:05 PM
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.
The below screen is the second input when i changed the (Fiels1 - OS Host Name), but Inventory name did not change.
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 05:16 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 05:25 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2017 05:32 AM
I'm glad you got it clarified and the solution I provided worked for you. Thank you for using the community.