- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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!