Getting display values of a list field to display in an infomessage

Davie
Giga Expert

find_real_file.png

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.

find_real_file.png

So if a user changes the Company i display the name of the previous Service as changing the Company also clears the Service.  

find_real_file.png

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', '');
}
}
}

find_real_file.pngconsole.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 :

find_real_file.png

grps[i].name and grps[i.name] didn't work, undefined 😞

1 ACCEPTED SOLUTION

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

View solution in original post

9 REPLIES 9

The SN Nerd
Giga Sage
Giga Sage

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

@Paul Morris thanks Paul...i think i actually tried that and it failed.  I tried your version and that failed also.  No values returned, i fear as the list has multiple values it in.  It is extremely similar to what i have for the Service field tho.

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', '');
}
}
}

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

thanks @vkachineni,  i'd already spotted and corrected that