Output printing as [object object]

praaaaaaaa
Kilo Contributor

How can i see the output of object variable in log. I tried to print the value of object variable but it is printing as [object object] see the screen shot below. How can i see the value?

find_real_file.png

8 REPLIES 8

The SN Nerd
Giga Sage
Giga Sage

I'd highly recommend using Xplore to read objects.



ServiceNow Share



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

Kyle
Giga Contributor

The simplest way to debug such a value is to serialize it using JSON.

Here is an example when dealing with glide records:

var gr = new GlideRecord('incident');
gr.get('14751599dbb9e740713861f74b961987');
var result = {};
for (var prop in gr) {
  result[prop] = gr.getValue(prop);
}
JSON.stringify(result);

You can then gs.info or w.e you need to do.

 

Simple objects you can just serialize:

var obj = { foo: 'bar' };

gs.info(JSON.stringify(obj));

 

As others have mentioned if the record already exists you can just use Xplore (extremely useful dev tool).

var gr = new GlideRecord('sys_email');
gr.get('602d2711dbce2f400da080c74b96191c');
gr;

 

Mike322
Tera Contributor

Hi Kyle,

This seems like a good solution but I've got an issue that I can't seem to resolve.

When I stringify my object, it truncates the string by removing whitespaces. I need to keep the whitespaces though for my query to work. How can I resolve this?

For example it should return this:

{"TOTAL":"5","TYPE":"guest","ID":"\n      498cb221cb91d9829fd77ff04a64beae\n      Agent\n    "}

But it returns this instead:

{"TOTAL":"5","TYPE":"guest","ID":"\n 498cb221cb91d9829fd77ff04a64beae\n Agent\n "}

 

Divya Rajasekar
Tera Contributor
JSON.stringify(object value);