g_form.getValue() return the value and not the name.

Santhana Rajhan
Mega Sage

Below is the code i use for the record producer. I need to get the Name but it returns the value.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
alert ('load');
var choice = g_form.getValue('pay_discrepancy_type');
alert (choice);
g_form.setValue('pay_dependancy_inquiry', 'Please ' + choice + ' attach');

}

10 REPLIES 10

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I would try something like this:

var fieldName = 'pay_discrepancy_type';
var fieldValue = g_form.getValue(fieldName);
var choice = g_form.getOption(fieldName, fieldValue).text;

https://docs.servicenow.com/bundle/kingston-application-development/page/app-store/dev_portal/API_re...

Thank you for responding Brad,

Getting an error "There is a JavaScript error in your browser console"

The pay_discrepancy_type is mapped to a field. Could this be the reason?

Could you post the script whole you're using (incorporating my suggestion) and the error from the console? Is the variable you're running this on a select box or a reference variable displayed as a choice list?

It is a multiple choice field mapped to a field. Below is the script.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// alert ('load');
var fieldName = 'pay_discrepancy_type';
var fieldValue = g_form.getValue(fieldName);
var choice = g_form.getOption(fieldName, fieldValue).text;
// alert (choice);
g_form.setValue('pay_dependancy_inquiry', 'Please ' + choice + ' attach');
}

Below are the field and error screenshots

find_real_file.png

find_real_file.png