why we need to convert it into to string by toStringMethod() without to string its value are not get

adityahubli
Tera Contributor
var scriptIncludeMultiRecord = Class.create();
scriptIncludeMultiRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {
multiRecord:function()
{
    var arr=[];
    var gr=new GlideRecord('incident');
    var user=this.getParameter('sysparm_user');
    gr.addQuery('caller_id',user);
    gr.query();
    while(gr.next())
    {
        var obj={};
        obj.number=gr.number.toString();
        obj.description=gr.description.toString();
        obj.short_description=gr.short_description.toString();
        arr.push(obj);

 

    }
    return JSON.stringify(arr);
},
    type: 'scriptIncludeMultiRecord'
});  
 
why we need to convert it into to string by toStringMethod() without to string its value are not getting 
 
 
 
3 REPLIES 3

M Iftikhar
Kilo Sage

Hi @adityahubli , 

You need .toString() because gr.number, gr.description, etc. are GlideElement objects, not plain JavaScript strings. If you put them directly into your array, JSON.stringify() can’t handle them and you’ll just get [object Object]. 

By using .toString() (or .getValue()), you convert the GlideElement into a normal string that can be serialized properly. 

Example: 

obj.number = gr.getValue("number"); 
 This is the same as gr.number.toString(), but cleaner. 

 

Use .getValue() when you want the raw field value and .getDisplayValue(), if you need the user-friendly display text (like for reference fields). 

Thanks & Regards, 
Muhammad Iftikhar

 

If my response helped, please mark it as the accepted solution so others can benefit as well. 

Ankur Bawiskar
Tera Patron
Tera Patron

@adityahubli 

either you need to use toString() or use getValue() on glideRecord field value to convert that to string.

when you use gr.short_description it give you GlideElement object and not the actual string value

good explanation here

TNT: The Importance of Using "getValue()" when Getting Data from a GlideRecord 

Is GlideRecord getValue the King of the String 

Also you should always use getValue() and setValue() on GlideRecord object

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Astik Thombare
Tera Sage

Hi @adityahubli ,

 

In your code, when you write:

 

obj.number = gr.number;

 

you’re not actually getting the string value of the field. Instead, you’re assigning a GlideElement object (the wrapper around the field) into your object. That’s why when you later stringify and return it, the values don’t show up properly.

 

To get the actual field value, you need to use one of the GlideRecord getter methods like:

 

gr.number.toString()

gr.getValue('number')

 

Both give you the real string value of the field, which is why your code works when you call .toString().

 

👉 In short: GlideRecord fields are objects, not plain strings, so you need to use getters (like .getValue() or .toString()) to extract the actual value.

 

You can read more about this best practice here:
🔗 Always use getters and setters – SN Pro Tips

 

If my answer helped you, please consider marking it as Correct and giving it a 👍 Helpful — it might help other members in the community too!

 

Thanks,
Astik T
ServiceNow Community Rising Star 2025 🌟😊💡