- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2019 11:45 AM
I have a script that take a value from a text variable, and splits it into an array. It works fine..except, I want the firat array to go into the first text variable, and any subsequent arrays to go into a second text variable.
So, if I get the value from VarA, I want to split it, and push it to VarB and VarC
If the user enters AAA BBB CCC DDD EEE in VarA
VarB shows AAA
VarC shows BBB CCC DDD because I don't have enough arrays defined.
------------------------------------------------------------------------------------------------
If the user enters AAA BBB in VarA
VarB shows AAA
VarC shows BBB undefined undefined because I only have two arrays - anything extra is undefined
What I would like to see is:
VarA = AAA
VarB = anything that come after the first split, no matter how many subsequent splits there are.
------------------------------------------------------------------------------------------------
Many thanks for all replies.....Mark S.
Script is:
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } var full_name = g_form.getValue('VarA'); var array = full_name.split(" "); var firstname1 = array[0]; var lastname2 = array[1]; var lastname3 = array[2]; var lastname4 = array[3]; g_form.setValue('VarB', array[0]); g_form.setValue('VarC', lastname2 + " " + lastname3 + " " + lastname4); } |
Solved! Go to Solution.
- Labels:
-
Request Management
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2019 01:44 PM
This works just fine now. Many, many thanks!! This is the finished script:
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } var full_name = g_form.getValue('account_display_name'); var array = full_name.split(" "); var firstname1 = array[0]; var x=''; for(var i=1;i<firstname1.length;i++) { x=x+' '+array[i]; } g_form.setValue('u_first_name', array[0]); g_form.setValue('u_last_name', full_name.split(array[0])[1].trim()); g_form.setReadOnly('u_first_name', true); g_form.setReadOnly('u_last_name', true); } |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2019 11:52 AM
Hi,
Try the below code
var full_name = g_form.getValue('VarA');
var array = full_name.split(" ");
var firstname1 = array[0];
var x='';
for(var i=1;i<firstname1.length;i++)
{
x=x+' '+array[i];
}
g_form.setValue('VarB', array[0]);
g_form.setValue('VarC', x);
Regards
Ravindra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2019 12:07 PM
Hello Ravinda, many thanks for you reply. Lets see how this works out in reality. This is a screen shot of what is happening with the new solution..you can see the 44444 and 5555555 is being cut off. My new script is below the screenshot.
Cheers, Mark
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } var full_name = g_form.getValue('account_display_name'); var array = full_name.split(" "); var firstname1 = array[0]; var x=''; for(var i=1;i<firstname1.length;i++) { x=x+' '+array[i]; } g_form.setValue('u_first_name', array[0]); g_form.setValue('u_last_name', x); g_form.setReadOnly('u_first_name', true); g_form.setReadOnly('u_last_name', true); } |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2019 12:13 PM
Hi,
Can you just check the length of the field and get back
Regards
Ravindra

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2019 01:14 PM
Here you go
var full_name = g_form.getValue('VarA');
var array = full_name.split(" ");
g_form.setValue('VarB', array[0]);
g_form.setValue('VarC', full_name.split(array[0])[1].trim());