onsubmit client script

levino
Giga Guru

Hi there

 

on a catalog item i have two fields


one is a reference field  sys_user - user_list


another is a free text field - service_account_users

 

both are non mandatory


i need a onsubmit script which stores the value on another single text field 'combined_list' as comma separated values


possible scenarios

user_list & service_account_users
1) both fields are populated
2) any one of the fields are populated
3) both fields are not populated

please provide me with a example

Thank You

Levino

1 ACCEPTED SOLUTION

@levino 

I updated it with all 4 scenarios; enhance it as per your requirement

function onSubmit() {
	var user = g_form.getValue('user_list');
	var service = g_form.getValue('service_account_users');

	if(user!='' && service!='')
	{
		g_form.setValue('combined_list', user + service); 
	}
	else if(user != '' & service == ''){
		// add your logic
		g_form.setValue('combined_list', user);
	}
	else if(user == '' && service != ''){
		// add your logic
		g_form.setValue('combined_list', service);
	}
	else if(user == '' && service == ''){ // both are empty so you decide what needs to be done
		// add your logic
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

6 REPLIES 6

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Your code would look like below:-

 

function onSubmit() {
var user;
var service;
if(g_form.getValue('user_list')!='')
{
    user=g_form.getValue('user_list') 
}
if(g_form.getValue('service_account_users')!='')
{
   service=g_form.getValue('service_account_users')
}
   var combined=user + service;
g_form.setValue('combined_list',combined);
}

 

Please mark my answer as correct based on Impact.

Ankur Bawiskar
Tera Patron
Tera Patron

@levino 

didn't get your use-case

When do you want the another single line text variable to be populated? and also with what value? based on which field?

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

Hi Ankur 

to be populated onsubmit.  

the below script does work to some extent that is if both fields are populated, if either one is populated it

comes up  'abc,xyz,undefined' i guess the below script needs to be amended to cater for that scenario

 

Thanks

Levino

function onSubmit() {
var user;
var service;
if(g_form.getValue('user_list')!='')
{
    user=g_form.getValue('user_list') 
}
if(g_form.getValue('service_account_users')!='')
{
   service=g_form.getValue('service_account_users')
}
   var combined=user + service;
g_form.setValue('combined_list',combined);
}

 

 

Hello,

 

Please try the below code once:-

 

function onSubmit() {
var user;
var service;
if(g_form.getValue('user_list')!='')
{
    user=g_form.getValue('user_list') ;
}
else
{
 user='';
}
if(g_form.getValue('service_account_users')!='')
{
   service=g_form.getValue('service_account_users')
}
else
{
 service='';
}
   var combined=user + service;
g_form.setValue('combined_list',combined);
}

 

Please mark my answer as correct based on Impact.