- 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 06:47 AM
You have forgotten to declare the variable 'i'.
I have removed it from your code.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//If the page isn't loading
if (!isLoading) {
//get Approval groups at time of company change
var displayValue = 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: " + displayValue);
} else if (newValue != oldValue) {
g_form.setValue('u_cab1', '');
}
}
}
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 08:02 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2020 08:11 AM
Minor change to Paul's code. Line console.log was logging the var grps instead of displayValue
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//If the page isn't loading
if (!isLoading) {
//get Approval groups at time of company change
var displayValue = g_form.getDisplayBox('u_cab1').value;
console.log('grps - ' + 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 08:16 AM
thanks @vkachineni, i'd already spotted and corrected that