- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2020 06:30 AM
Swap sys_id's for arrays display values
I am building a lookup table for approval groups based on the service the change request it for.
We can have multiple groups as the approvers but if a user decides to do a insert & stay or change i want to display the names of the groups previously selected.
So if a user changes the Company i display the name of the previous Service as changing the Company also clears the Service.
I want to do the same with the Approval groups but can't quite get it
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//If the page isn't loading
if (!isLoading) {
//get Approval groups at time of company change
var grps = [];
grps.push(g_form.getDisplayBox('u_cab1').value);
console.log('grps - ' + grps);
// notify user that approvals groups cleared but advise what it was
if (newValue != oldValue) {
g_form.setValue('u_cab1', '');
g_form.showFieldMsg("u_cab1", "Approval Group(s) cleared as Company changed, previously: " + grps[i]);
} else if (newValue != oldValue) {
g_form.setValue('u_cab1', '');
}
}
}
console.log shows = grps -
trying grps.push(g_form.getDisplayValue('u_cab1')); gives me this display value of the current record not the approval groups
however grps.push(g_form.getValue('u_cab1')); gives me the groups sys_id's :
grps[i].name and grps[i.name] didn't work, undefined 😞
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2020 05:16 PM
Glad to hear you got it working.
You might not be aware, using GlideRecord in Client Scripts makes a synchronous server-side call (will block the browser for a moment) and returns the data for the entire record. If you had 5 groups previously in the list, that will make for 5 server-side calls that will block the browser.
I'd recommend using an onDisplay Business rule instead and passing the details to the scratchpad.
On display business rule:
(function executeRule(current, previous /*null when async*/ ) {
g_scratchpad.previous_groups = current.u_cab1.getDisplayValue();
})(current, previous);
Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//If the page isn't loading
if (!isLoading && newValue != oldValue) {
g_form.setValue('u_cab1', '');
g_form.showFieldMsg("u_cab1", "Approval Group(s) cleared as Company changed, previously: " + g_scratchpad.previous_groups);
}
}
Isn't that cleaner!
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2020 09:16 AM
Try this .
Make sure the Isolate script is false for the client script.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//If the page isn't loading
if (!isLoading) {
//get Approval groups at time of company change
//TODO: GET THE EXACT NAME AS TABLE.FIELD in the gel call.
var displayValue = gel('incident.u_cab1').innerHTML;
console.log('@@@@@displayValue - ' + displayValue);
// notify user that approvals groups cleared but advise what it was
if (newValue != oldValue) {
g_form.setValue('u_cab1', '');
g_form.showFieldMsg("u_cab1", "Approval Group(s) cleared as Company changed, previously: " + displayValue);
} else if (newValue != oldValue) {
g_form.setValue('u_cab1', '');
}
}
}
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2020 06:47 AM
I think the best way to achieve this is by using Glide Ajax. Send the sysid to the script include, query the tables and return all the display values in an array.
or as these are reference fields you can try using g_form.getReference()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2020 02:59 AM
here is the client script needed:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//If the page isn't loading
if (!isLoading) {
var grps = []; //array of group sys_ids
grps.push(g_form.getValue('u_cab1'));
var b = grps.toString(); //array to string for lookup
if (newValue != oldValue && grps != '') {
var myString1Split = b.split(',');
var str = '';
for (i = 0; i < myString1Split.length; i++) {
var mylst = new GlideRecord('sys_user_group');
mylst.addQuery('sys_id', myString1Split[i]);
mylst.query();
while (mylst.next()) {
str += mylst.name + ', ';
}
}
g_form.setValue('u_cab1', '');
g_form.showFieldMsg("u_cab1", "Approval Group(s) cleared as Company changed, previously: " + str);
} else if (newValue == oldValue && grps == '') {
g_form.setValue('u_cab1', '');
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2020 05:16 PM
Glad to hear you got it working.
You might not be aware, using GlideRecord in Client Scripts makes a synchronous server-side call (will block the browser for a moment) and returns the data for the entire record. If you had 5 groups previously in the list, that will make for 5 server-side calls that will block the browser.
I'd recommend using an onDisplay Business rule instead and passing the details to the scratchpad.
On display business rule:
(function executeRule(current, previous /*null when async*/ ) {
g_scratchpad.previous_groups = current.u_cab1.getDisplayValue();
})(current, previous);
Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//If the page isn't loading
if (!isLoading && newValue != oldValue) {
g_form.setValue('u_cab1', '');
g_form.showFieldMsg("u_cab1", "Approval Group(s) cleared as Company changed, previously: " + g_scratchpad.previous_groups);
}
}
Isn't that cleaner!
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2020 05:12 PM
Thanks
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022