Get Display Values in Client Scripts
As we all know that it's always been a concern to get the display value of different fields/variables in client scripts. So, this little effort is based on my learning to demonstrate that how to get display value of different fields/variables given below.
- Select Box Variable / Choice Field
- Reference Variable / Reference Field
- Lookup Select Box Variable
- List Collector Variable
Note: In this implementation objects like window to distinguish between Service Portal and Native UI is not used, rather a different approach is used in which you do not have to uncheck (make false) the Isolate script checkbox.
Service Portal Result
Native UI Result
Note: In below catalog client script, display value for List Collector and Lookup Select Box in Native UI does not work.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// For Selectbox Variable or Choice Field
var isPortal = g_form.getDisplayValue('type');
if (isPortal.length != 0) {
g_form.showFieldMsg('type', 'New Backend Value: ' + newValue + ' New Display Value: ' + g_form.getDisplayValue('type'));
} else {
g_form.showFieldMsg('type', 'New Backend Value: ' + newValue + ' New Display Value: ' + g_form.getOption('type', newValue).text);
g_form.showFieldMsg('type', 'Old Backend Value: ' + oldValue + ' Old Display Value: ' + g_form.getOption('type', oldValue).text);
}
// For Reference Variable
isPortal = g_form.getDisplayValue('user');
if (isPortal.length != 0) {
g_form.showFieldMsg('user', 'New Display Value: ' + g_form.getDisplayValue('user'), 'info', true);
} else {
g_form.showFieldMsg('user', 'New Display Value: ' + g_form.getDisplayBox('user').value, 'info', true);
}
// For Lookup Selectbox Variable
isPortal = g_form.getDisplayValue('lookup_selectbox');
if (isPortal.length != 0) {
g_form.showFieldMsg('lookup_selectbox', 'New Display Value: ' + g_form.getDisplayValue('lookup_selectbox'), 'info', true);
} else {
// Control comes here if item is opened in Native UI.
// This does not work for List Collector.
g_form.showFieldMsg('lookup_selectbox', 'New Display Value: ' + g_form.getDisplayBox('lookup_selectbox').value, 'error', true);
}
// For List Collector Variable
isPortal = g_form.getDisplayValue('list_collector');
if (isPortal.length != 0) {
g_form.showFieldMsg('list_collector', 'New Display Value: ' + g_form.getDisplayValue('list_collector'), 'info', true);
} else {
// Control comes here if item is opened in Native UI.
// This does not work for List Collector.
g_form.showFieldMsg('list_collector', 'New Display Value: ' + g_form.getDisplayBox('list_collector').value, 'error', true);
}
}
Interested in reading my other articles, go through the below link.
Useful Implementation for Service Portal and Native UI
Always open to learn new things.
Happy Learning
https://www.servicenow.com/community/developer-articles/get-display-values-in-client-scripts/ta-p/2317676