
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2019 03:02 AM
Good morning all,
We are building a new starter catalogue request and need to populate a contact email variable based on the location of the user. Within a variable set, we have a reference field, starter_location, which is filtered to show just 4 specific locations, say north, south, east and west. If the new starters location is north, we want to automatically populate a single line text field, starter_email, with a defined email address. Note this email is not part of the user record, otherwise we could have referenced it from the sys_user table.
I have the following on change client script, but its not working. Is there anything obvious wrong with this approach please:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setValue('starter_email,"");
}
var loc = g_form.getValue('starter_location');
if(loc =='North'){
g_from.setValue('starter_email',"north@xyz.com");
}
else if(loc =='South'){
g_from.setValue('starter_email',"south@xyz.com");
}
else if(loc =='East'){
g_from.setValue('starter_email',"east@xyz.com");
}
else if(loc =='West'){
g_from.setValue('starter_email',"west@xyz.com");
}
return;
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2019 06:56 AM
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var val=g_form.getDisplayBox('cmdb_ci').value;
if(val=='All')
{
alert('inside');
g_form.setValue('specific_transaction','1232131231');
}
//Type appropriate comment here, and begin script below
}
I wrote this and it was able to set the value in the specific_transaction field. Can you check if the field in which you are setting the value has the correct data type.
Regards,
Gundeep Singh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2019 06:56 AM
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var val=g_form.getDisplayBox('cmdb_ci').value;
if(val=='All')
{
alert('inside');
g_form.setValue('specific_transaction','1232131231');
}
//Type appropriate comment here, and begin script below
}
I wrote this and it was able to set the value in the specific_transaction field. Can you check if the field in which you are setting the value has the correct data type.
Regards,
Gundeep Singh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-30-2019 01:01 AM
Gundeep,
Your help on this is much appreciated, but still not working. I even created a new request with just two fields, location and result, but still no joy. Have even tried location as both a reference field and a look up select box.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2019 04:11 AM
Gundeep,
You will be pleased to hear that I finally got your script working. Appears the problem was the double quotation around the set value
Thanks again

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2019 06:58 AM
HI Cirus,
It is good to hear that , even i was wondering that what else can be there and i was thinking about it over and over.
Regards,
Gundeep Singh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2019 03:17 AM
Your 'starter_location' field is a reference field, which is stored as a sys_id in the database.
Using sys_id's in client scripts isn't considered best practice by ServiceNow, so you might want to consider options below:
-Use g_form.getDisplayBox().value is one option, however, I don't think it is supported in Service Portal (it is not listed here)
-Use a Choice List variable instead, that points at the table containing North, South, East and West, with the same filter that was applied to the reference variable. Then, your code should work.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022