- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2020 10:35 AM
I have a variable on a catalog item, 'location_name_ref', that is a reference to the cmn_location table. I have a single line text variable, 'site_address'. I believe an onChange script is needed to pull the 'street', 'city', 'state', 'zip' label display values from the 'location_name_ref' selection to the 'site_address' variable, but I'm not sure what that code looks like. We have something similar from a previous employee that pulls a user's email from a different reference variable to a single line text variable:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if(!isLoading && oldValue != newValue){
g_form.getReference('tech_contact_ref', getUserInfo);
}
}
function getUserInfo(userInfo){
g_form.setValue('technical_contact_email',userInfo.email);
}
For clarity, I would like to select ABC as the referenced location and have it auto populate the street, city, state, and zip from that location in another variable.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2020 11:24 AM
Hi Scott,
Try the below onChange client script on 'location_name_ref' variable.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
//Type appropriate comment here, and begin script below
if(newValue == ''){
g_form.clearValue('site_address');
}else{
var location = g_form.getReference('location_name_ref', callBack);
}
}
function callBack(location){
var site = location.street;
var city = location.city;
var state = location.state;
var zip = location.zip;
var address = site+"\n"+city+"\n"+state+"\n"+zip;
g_form.setValue('site_address', address);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2020 06:32 AM
Hi Scott,
I agree with Mike's comment using GlideAjax is much more efficient than using getReference() with callback method
Please consider marking appropriate reply as ✅Correct & 👍Helpful.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 07:55 AM