- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2023 11:19 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 04:29 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2023 11:34 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2023 11:36 PM
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?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 04:22 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 04:27 AM
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.