
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2017 02:09 PM
I've created a client script to populate fields onChange from a custom table. The field is returning a sys_id and not locating the vendor name.
How can I return the display value for the sys_id
Here is my client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var existingVendor = g_form.getValue('existing_vendor');
var venRec = new GlideRecord ('u_business_impact_analysis'); //Look at BIA table and find existing vendor
venRec.addQuery('u_vendor_name', existingVendor);
venRec.query(mycallback);
function mycallback(venRec){
while (venRec.next()){
//Populate form txt fields from BIA table.
g_form.setValue('system_owner', venRec.u_system_owner);
g_form.setValue('asset_type', venRec.u_asset_type);
g_form.setValue('product_scope', existingVendor);
}
}
}
Table:
The Vendor Name is a string field for when a new vendor is entered and i'm attempting to retrieve the display value of this field in the client script.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2017 02:48 PM
I found a solution to get the display values as needed. I had to set the value for each variable in an if statement.
Thank you for your assistance.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2017 08:56 PM
Hi Kristy,
Looking @ the provided table screenshot Vendor Name is a string field and you trying to pass sys_id in that. You need to have GlideAjax to fetch the data.
var existingVendor = g_form.getValue('existing_vendor'); Is existing_vendor a reference field?
var venRec = new GlideRecord ('u_business_impact_analysis'); //Look at BIA table and find existing vendor
venRec.addQuery('u_vendor_name', existingVendor); If existing_vendor is a reference field then you are passing sys_id to string field since Vendor Name is a string field
venRec.query(mycallback);
function mycallback(venRec){
while (venRec.next()){
//Populate form txt fields from BIA table.
g_form.setValue('system_owner', venRec.u_system_owner); // You need to use getDisplayValue() but not sure if it works client scripting
g_form.setValue('asset_type', venRec.u_asset_type);
g_form.setValue('product_scope', existingVendor);
}
}
I think it's recommended to use GlideAjax to fetch the data from server side, please refer this on how to fetch multiple data using GlideAjax: GlideAjax - ServiceNow Wiki

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2017 02:16 PM
Hi Kristy,
Instead of GlideRecord, please user getReference to get the details of existing_vendor. please find more details here: GlideForm (g form) - ServiceNow Wiki
Please find the Example below:
function onChange(control, oldValue, newValue, isLoading) {
var caller = g_form.getReference('existing_vendor', doAlert); // doAlert is our callback function
}
function doAlert(caller) { //reference is passed into callback as first arguments
g_form.setValue('system_owner', caller.u_system_owner);
g_form.setValue('asset_type', caller.u_asset_type);
g_form.setValue('product_scope', existingVendor);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2017 08:48 AM
I've got most of this working by calling the sys_id without single quotes around my variable.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var existingVendor = g_form.getValue('existing_vendor');
var venRec = new GlideRecord ('u_business_impact_analysis'); //Look at BIA table and find existing vendor
venRec.addQuery('sys_id', existingVendor);
venRec.query(mycallback);
function mycallback(venRec){
while (venRec.next()){
//Populate form txt fields from BIA table.
g_form.setValue('system_owner', venRec.u_system_owner);
g_form.setValue('product_scope', venRec.u_product_scope);
//g_form.setValue('asset_type', venRec.u_asset_type);
//g_form.setValue('data_classification', venRec.u_data_classification);
//g_form.setValue('vendor_rating', venRec.u_vendor_rating);
//g_form.setValue('product_rating', venRec.u_product_rating);
g_form.setValue('vendor_services_provided', venRec.u_vendor_services_provided);
g_form.setValue('rpo', venRec.u_rpo);
}
}
}
The next issue, how do I get the select boxes to populate as the .getDisplayValue() is not working in the client script.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2017 09:44 AM
Can you send a screenshot of a record in u_business_impact_analysis table?
You are querying u_business_impact_analysis with the sys_id of existingVendor.
venRec.addQuery('sys_id', existingVendor);
Check your variable 'existing_vendor', if it is pointing to u_business_impact_analysis or to some other table
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2017 09:50 AM