Can I use get display value in client script?

kiki330611
Kilo Expert

Hi everyone,

I am writing a client script in record producer to auto-fill a few user info fields based on the requester name (Your_name) entered. (The requester field is NOT a reference field)

I managed to write something below to find the requester name in user table and get the value for email, company and location :

find_real_file.png

My problem is the company and location fields in user table are reference field so they returned sys id...

I tried using gr.getDisplayValue but it doesn't seem to work in client script...

Can anyone suggest a way to get the display value for those two fields so I can auto-populate them in actual names not in sys id?

(It's returning like below now...)

Record.jpg

Thank you!

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

I   recommend doing this with an Ajax call. You need more than just the user's information in this case. Using a GlideRecord query or getReference() will work, but you'll have to make multiple calls. GlideAjax (and the corresponding server side script include) can be more surgical.



Hint: return a JSON object with sys_id and display value and use three arguments for your g_form.setValue() otherwise, you're wasting another return trip to get the display values.



GlideAjax - ServiceNow Wiki


View solution in original post

8 REPLIES 8

Mihir Mohanta
Kilo Sage

You cant use getDisplayValue() function in client script.



You need to use getDisplayBox() function.


Please go through below threads.



Please go through below threads.



Re: getValue on a reference field



Re: catalog client script onsubmit




Thanks,


Mihir


Deepa Srivastav
Kilo Sage


Use Ajax call for this :-


example code..



var SetDepartment = Class.create();


SetDepartment.prototype =
Object.extendsObject(AbstractAjaxProcessor, {


                           
CallerDeptDetails: function() {                                    


                                                           
var caller =this.getParameter('sysparm_user_caller');                                                            
var callerDetails = new GlideRecord('sys_user');                                                            
callerDetails.addQuery('sys_id',caller);                                                            
callerDetails.query();                                                            
if(callerDetails.next())                                                                                            
{                                                                                                                            
dept = callerDetails.department.name;                                                                                                                                                                                                                      
}


return dept;  
},                                                                                      


                           
});



Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.


Thanks,
Deepa


Igor __tek
Tera Contributor

Hi, 

with the field Isolate script set to false on the client script form you can get the display value like this:

document.getElementById("sys_display." + g_form.getElement('field_name').id).value;

Hope it helps.

Don't touch isolate script, let it be at its default value.

Use the below to get the display value of reference field 

g_form.getDisplayBox('<field name>').value

-Anurag