toString method not returning a string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2021 01:01 PM
I'm expecting for the below to return a string, but instead it's returning a sysid. Can anyone see what I'm doing wrong?
var manager = current.variables.requested_for.manager.toString();
Thank you.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2021 01:27 PM
Hi, a reference record will return it's sys_id using toString().
try using getDisplayValue('fieldname');
https://developer.servicenow.com/dev.do#!/reference/api/quebec/server_legacy/c_GlideRecordAPI#r_GlideRecord-getDisplayValue?navFilter=getdisplay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2021 01:28 PM
Reference fields contain the sys_id value of the record in the referenced table. There is a 'getDisplayValue()' method in the GlideRecord class. That may help, but I see you are dot-walking for levels. I haven't seen that scenario myself.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2021 01:41 PM
// get an incident that refers to a printer
var gr = new GlideRecord('incident');
gr.addQuery('u_serial_number', '!=','' );
gr.query();
gr.next();
var sys_id = gr.sys_id;
// print the raw value of the reference column u_serial_number
gs.info( "1. " + gr.u_serial_number ); // "1. 004f47b1db90e014023fe66505961913"
gs.info( "1a. " + gr.u_serial_number.toString() ); // "1a. 004f47b1db90e014023fe66505961913"
// print the display value of the same column
gs.info( "2. " + gr.getDisplayValue('u_serial_number') ); // "2. Lexmark CX860dte"

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2021 01:44 PM
Hi,
"String" can be a sys_id...that's the JavaScript variable typeof classification.
So I think you may be using the wrong wording here, but you're looking for a manager name? Or something like that...so you'd have to dot-walk to that information otherwise, you're just retrieving reference field information from the requested_for's user record and their respective manager field.
You'd want use:
var manager = current.variables.requested_for.manager.name.toString();
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!