Client Script returning 'undefined' in portal view, working fine in the ITIL view

Rick H
Tera Contributor

We've got a new requirement to populate an info tooltip to clarify the Line Leader/Manager linked to an Asset on a Catalogue Item. Seemed a relatively simple ask, and I was able to deliver a few different solutions that work fine in the ITIL/Admin/Desktop view in ServiceNow, but when testing the same item and same client script in the Portal view, it doesn't work.

 

The value returns as Undefined, and when trying to investigate I found that in the admin view the object returns as [object Object] while in the portal it returns as GlideRecord (see attached alert output)

 

The function in question is in an OnChange Client Script and looks like this:

function managerFunction(input){
   while (input.next()) { //While the recordset contains records, iterate through them
	var managergs = new GlideRecord('sys_user');
	managergs.get(input.manager);
	alert("Manager: " + managergs);
	manager = managergs.name;

	//Display assigned_to.manager name on the Catalogue Item form
	g_form.setValue('line_leader',manager);
	//g_form.showFieldMsg('serial_number', 'Line Leader: ' + manager, 'info', 'true');
	//g_form.showFieldMsg('manager_approval_checkbox', 'Approver: ' + manager, 'info', 'true'); 
	
   }

 

 What is the scope difference preventing this function from returning the manager name value in the Portal, and how can I best resolve it within this Client Script?

1 ACCEPTED SOLUTION

Hello @Rick H ,

                             But GlideRecord wont work on Service Portal. In this case you can go for getReference() method if it fulfil your requirement. Please check 

How to get Manager's name, email from a contact record? (Contact field referring to sys_user) 

How To Use g_form.getReference() To Dot Walk In A Client Script 

Kindly mark correct and helpful if applicable 

View solution in original post

3 REPLIES 3

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi @Rick H ,

                      First of all do not use GlideRecord in Client Script its not best practice in ServiceNow because The GlideRecord Client side method may request several Records (that's right, the whole record, big Objects with all fields values) to the server, following the addQuery condition. It may cause slowness to your Client Side action.

Best way to call from Server data is using GlideAjax API

you can refer this article for more details GlideAJAX that can be used by client scripts and replace existing use of GlideRecord from Client scr... 

 

Kindly mark correct and helpful if applicable 

Thanks Chetan,

 

My team and I don't have access to create Script Includes in order to define a custom class to reference with GlideAjax API in the Client Script, is there a solution I can implement in the Client Script itself that achieves the same result as the get() call I used on the GlideRecord?

Hello @Rick H ,

                             But GlideRecord wont work on Service Portal. In this case you can go for getReference() method if it fulfil your requirement. Please check 

How to get Manager's name, email from a contact record? (Contact field referring to sys_user) 

How To Use g_form.getReference() To Dot Walk In A Client Script 

Kindly mark correct and helpful if applicable