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

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', '');
		}
	}
}
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Chavan AP
Kilo Sage

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()

Glad I could help! If this solved your issue, please mark it as ✅ Helpful and ✅ Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****

Davie
Giga Expert

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

 

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

Thanks @Davie ðŸ™‚


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022