- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2023 06:17 AM
Scenario: I am Gliding a table for example 'incident', after applying query there are should be 10 incidents displayed.
Expected output: All 10 record should be displayed one by one with required details.
Output:
{
"Number": Incident1,
"Category": cat,
"State": state,
"Short Description": sd,
"Description": des
},
{
"Number": Incident2,
"Category": cat,
"State": state,
"Short Description": sd,
"Description": des
},
and so on
Please help @Ankur Bawiskar @Community Alums @Allen Andreas @BharathChintala
Used script:
var gr = new GlideRecord('incident');
gr.addQuery('Active',true);
gr.query();
while(gr.next()){
var num = gr.number;
var cat = gr.category.getDisplayValue();
var state = gr.state.getDisplayValue();
var sd = gr.short_description.getDisplayValue();
var des = gr.description.getDisplayValue();
}
return {
"Number": num,
"Category": cat,
"State": state,
"Short Description": sd,
"Description": des
};
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2023 01:06 PM - edited 03-03-2023 06:37 AM
Hi,
You'd need to apply a query to your GlideRecord script to target specific records, as you mentioned in your post. You can navigate to list view of incidents, build your query, then right-click the last piece of the breadcrumb and use an additional line (right above your gr.query();) such as:
gr.addEncodedQuery("paste_here");
As far as building an array of data, you could use:
var array = [];
var gr = new GlideRecord('incident');
//gr.addEncodedQuery("paste_here");
gr.addQuery('active',true);
gr.setLimit(10);
gr.query();
while(gr.next()){
var obj = {
"Number": gr.getValue('number'),
"Category": gr.category.getDisplayValue(),
"State": gr.state.getDisplayValue(),
"Short Description": gr.short_description.getDisplayValue(),
"Description": gr.description.getDisplayValue()
}
array.push(obj);
}
gs.info(JSON.stringify(array, null, 2));
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2023 03:42 AM
Hi @India_farheenk ,
Use below script. It will work.
var array = [];
var gr = new GlideRecord('incident');
gr.addQuery('active',true);
gr.setLimit(10);
gr.query();
while(gr.next()){
var obj = {
"Number": gr.getValue('number'),
"Category": gr.category.getDisplayValue(),
"State": gr.state.getDisplayValue(),
"Short Description": gr.short_description.getDisplayValue(),
"Description": gr.description.getDisplayValue()
};
array.push(obj);
}
var str = JSON.stringify(array);
var array = JSON.parse(str);
return array;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2023 01:06 PM - edited 03-03-2023 06:37 AM
Hi,
You'd need to apply a query to your GlideRecord script to target specific records, as you mentioned in your post. You can navigate to list view of incidents, build your query, then right-click the last piece of the breadcrumb and use an additional line (right above your gr.query();) such as:
gr.addEncodedQuery("paste_here");
As far as building an array of data, you could use:
var array = [];
var gr = new GlideRecord('incident');
//gr.addEncodedQuery("paste_here");
gr.addQuery('active',true);
gr.setLimit(10);
gr.query();
while(gr.next()){
var obj = {
"Number": gr.getValue('number'),
"Category": gr.category.getDisplayValue(),
"State": gr.state.getDisplayValue(),
"Short Description": gr.short_description.getDisplayValue(),
"Description": gr.description.getDisplayValue()
}
array.push(obj);
}
gs.info(JSON.stringify(array, null, 2));
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2023 06:38 AM
Please review my script above and my other reply, I've given you further assistance and it doesn't require looping or anything else, very simple adjustment.
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2023 06:45 AM
Your script is really helpful to me as well. It gives a shortcut to print in new line with JSON objects. Loved it!! Keep sharing!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2023 06:53 AM
You're welcome 🙂
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!