- 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 07:49 PM
Allen has shared the script.
Please try that and share feedback
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2023 03:07 AM
@Allen Andreas @Ankur Bawiskar
We are getting response in one line, please find the below screenshot:
We are trying in REST API Explorer.
- 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-03-2023 03:55 AM
USe this.
var array = [];
var len;
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);
len = array.length;
}
gs.info(JSON.stringify(array));
gs.info(len);
var spaces = [];
for(var i=0;i<len;i++)
{
spaces += JSON.stringify(array[i]) + "\n";
}
gs.info(spaces);
Hope this helps!
Regards,
Shamma

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2023 06:36 AM
Hello @India_farheenk
All you need to do is add a few parms in the output, like so:
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));
Which results in example:
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!