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
Administrator
Administrator

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

Allen Andreas
Administrator
Administrator

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!

Hi @India_farheenk 

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!

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!!

Regards,Shamma Negi

You're welcome 🙂


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