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

@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

Hrishabh Kumar
Giga Guru

Use the following code in your Onsubmit script section :

{
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);
}