Create a dynamic table from and array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2019 09:07 AM
Hi,
I need some assistance on creating a dynamic table from an array.
I want the configuration item to be a fixed header and the elements to be the custom headers with the state below.
The script so far and outcome:
var arr = [];
var ga = new GlideAggregate("cert_element");
ga.addQuery("cert_task", current.sys_id);
ga.query();
while (ga.next()) {
var obj = {};
obj.configuration_item= ga.configuration_item.toString(),
obj.element = ga.element.toString();
obj.state = ga.state.toString();
arr.push(obj)
}
gs.log(JSON.stringify(arr));
results:
[{"configuration_item":"5a40ea6ddb56e680067450cbbf961921","element":"u_environment","state":"Certified"},{"configuration_item":"5a40ea6ddb56e680067450cbbf961921","element":"u_affected_business_units","state":"Failed"},{"configuration_item":"5a40ea6ddb56e680067450cbbf961921","element":"u_lifecycle_state","state":"Failed"}]
Any advise on printing out on to a table would be much appreciated.
Ahmed
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2019 02:45 AM
Hi Ahmed,
Do you want to print this data in the form of a html table or create a db table with this data?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2019 02:47 AM
Hi Asif,
I want to print the data on a html table.
Thanks,
Ahmed

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2019 03:46 AM
Here is how you can do it
var arr = [{"configuration_item":"5a40ea6ddb56e680067450cbbf961921","element":"u_environment","state":"Certified"},{"configuration_item":"5a40ea6ddb56e680067450cbbf961921","element":"u_affected_business_units","state":"Failed"},{"configuration_item":"5a40ea6ddb56e680067450cbbf961921","element":"u_lifecycle_state","state":"Failed"},
{"configuration_item":"tstt","element":"u_lifecyclaaaae_state","state":"Failsssd"}]
gs.print(arr.length);
var myhtml="<html><body><table><th>Configuration Item</th><th>Element</th><th>Environment</th>";
for(i=0;i<arr.length;i++) {
myhtml = myhtml+"<tr><td>"+arr[i].configuration_item+"</td><td>"+arr[i].element+"</td><td>"+arr[i].state+"</td></tr>";
}
myhtml = myhtml+"</table></body></html>";
gs.print(myhtml);
Mark the comment as a correct answer and also helpful once worked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2019 04:41 AM
Thank you for your assistance.
Apologies if I wasn’t clear I was hoping to get format like this:
configuration item |
u_environment |
u_affected_business_units |
u_lifecycle_state |
5a40ea6ddb56e680067450cbbf961921 |
Certified |
Failed |
Failed |
So each element will be the header and the state the value below.
Thanks,
A