The CreatorCon Call for Content is officially open! Get started here.

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 ACCEPTED SOLUTIONS

M Iftikhar
Mega 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. 

View solution in original post

@adityahubli 

Hope you are doing good.

Did my reply answer your question?

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

View solution in original post

Bhimashankar H
Mega Sage

hi @adityahubli ,

 

Many ServiceNow APIs return GlideElement objects, not plain strings; without converting to a string (for example via toString(), getValue(), or String()), code that expects primitives can behave oddly, such as pushing references into arrays and later seeing all entries collapse to the last record. Converting do a stable string of the value at that moment.

 

Check out the below code snippet for difference.

var nums = [];
var gr = new GlideRecord('incident');
gr.orderByDesc('number');
gr.setLimit(3);
gr.query();
while (gr.next()) {
  nums.push(gr.number); // pushes a GlideElement, not a string
  //nums.push(gr.getValue('number')); //check by commenting above line
}
gs.info('Wrong nums: ' + nums); 

You can see the difference.

 

You can also verify using below code snippet.

gs.info(typeof(gr.number))  //=>Object

gs.info(typeof(gr.number.toString()))  //=>String

 

toString() Method helps to process the array element later when passed from server to client side.

 

Check this article for importance of toString()

Do not overlook GlideRecord fields: Think before you cast! 

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!

View solution in original post

3 REPLIES 3

M Iftikhar
Mega 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. 

@adityahubli 

Hope you are doing good.

Did my reply answer your question?

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

Bhimashankar H
Mega Sage

hi @adityahubli ,

 

Many ServiceNow APIs return GlideElement objects, not plain strings; without converting to a string (for example via toString(), getValue(), or String()), code that expects primitives can behave oddly, such as pushing references into arrays and later seeing all entries collapse to the last record. Converting do a stable string of the value at that moment.

 

Check out the below code snippet for difference.

var nums = [];
var gr = new GlideRecord('incident');
gr.orderByDesc('number');
gr.setLimit(3);
gr.query();
while (gr.next()) {
  nums.push(gr.number); // pushes a GlideElement, not a string
  //nums.push(gr.getValue('number')); //check by commenting above line
}
gs.info('Wrong nums: ' + nums); 

You can see the difference.

 

You can also verify using below code snippet.

gs.info(typeof(gr.number))  //=>Object

gs.info(typeof(gr.number.toString()))  //=>String

 

toString() Method helps to process the array element later when passed from server to client side.

 

Check this article for importance of toString()

Do not overlook GlideRecord fields: Think before you cast! 

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!