- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2017 07:49 AM
I have multiple fields on a form. I'm trying to get the value of a choice-list field called 'patient_category'. But, the following code always returns the value of another field on the form -- the patient_name field. regardless of what field I specify as the getDisplayValue parameter, the system always returns the value of the patient_name field. I'm totally new to scripting in ServiceNow so am wondering what I am missing.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
alert(g_form.getDisplayValue('patient_category'));
}
Thanks in advance for your help.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2017 09:14 PM
Hi Mike,
Can you try this code
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
alert(g_form.getDisplayBox('patient_category').value);
}
P.S: Hit Like or Correct depending on the impact of response
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2017 01:12 PM
Thanks for the reply. Unfortunately, the suggestions didn't solve the problem.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2017 02:10 PM
You can try this client script by changing <choicefield> to the name of your choice field.
This client script returns get all choice list values. Please modify accordingly.
- var values = [];
- var sel = g_form.getElement('<choicefield>');
- for (var i=0, n=sel.options.length;i<n;i++) {
- if (sel.options[i].value) values.push(sel.options[i].value);
- }
- alert(values.join(","));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2017 03:07 AM
As rightly mentioned by explorenow getDisplayValue() is a server side function and cannot be used in Client side. If you need to fetch the value of choice labels of a field better way would be to use a server side script and call the same on a client side.
For example : 1) In your scenario, write a Display Business rule on your target table with a scratchpad variable as mentioned below:
g_scratchpad.val = current.patient_category.getDisplayValue();
2) After step 1, write an on Load/On Change Client script and call the above scratchpad variable as mentioned below:
function onLoad() {
//Type appropriate comment here, and begin script below
alert(g_scratchpad.val);
}
This should work for you.
Cheers,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-25-2017 11:39 AM
Thank you for the reply. Really appreciate the additional information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2017 09:25 AM
Try using this.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue==="") {
return;
}
alert(g_form.getOption('u_patient_category', g_form.getValue('u_patient_category')).text);
}