Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to display the list of record one by one with required details?

India_farheenk
Giga Contributor

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

    };
2 ACCEPTED SOLUTIONS

Allen Andreas
Tera Patron

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!

View solution in original post

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;	

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@India_farheenk 

Allen has shared the script.

Please try that and share feedback

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

India_farheenk
Giga Contributor

@Allen Andreas @Ankur Bawiskar 

 

We are getting response in one line, please find the below screenshot:

We are trying in REST API Explorer.

 

India_farheenk_0-1677841601698.png

 

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;	

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

Regards,Shamma Negi

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:

AllenAndreas_0-1677854191060.png

 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!