How to get data from a table and iterate through it?

Ccart
Giga Expert

I want to get data from a pre-existing table and iterate through that table, putting each row into an Array.

array example:

tableData = [

[row1TableData],

[row2TableData],

];

How would I do this via Mid server script? 

1 ACCEPTED SOLUTION

Gowrisankar Sat
Tera Guru

var arr = [];

var gr = new GlideRecord("<table>");

gr.addQuery(<query here>);

while(gr.next())

{

//pushes only only one field value

arr.push(gr.<field_value>);

}

Check this community article as well:

https://community.servicenow.com/community?id=community_question&sys_id=7e3247addb98dbc01dcaf3231f96...

View solution in original post

2 REPLIES 2

Gowrisankar Sat
Tera Guru

var arr = [];

var gr = new GlideRecord("<table>");

gr.addQuery(<query here>);

while(gr.next())

{

//pushes only only one field value

arr.push(gr.<field_value>);

}

Check this community article as well:

https://community.servicenow.com/community?id=community_question&sys_id=7e3247addb98dbc01dcaf3231f96...

Kunal Varkhede
Tera Guru

Hi,

 

I want to add one point in above discussion that we can also do like below 

 

var arr = ['val1','val2','val3'];

for(var i = 0; i<arr.length; i++)

{

var gr = new GlideRecord('<Table>');

gr.addQuery('active','true');

//gr.addQuery('<field_value>,arr[i]);   //here you can also compare array of value with table fields value

gr.query();

if(gr.next())

{

 arr.push(gr.<field Value>);

}

}//for loop close

 

Thanks,

Kunal