How to fetch values from script include to ui page in tabular format?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 08:57 PM
Hello Team,
i have a script include where it is returning some values in a JSON and i need to create a table(tabular format with rows and columns) with those in a ui page
for ex --
Script_Include()
get _Values : function(){
return (Value);
let 'Value' returns like below
[{"student : Swetha"}, {"student : Navya"}, {"student : anu"}, {"student : kamal"}, {"student : krish"},]
output expected ---
Student |
Swetha |
Nadya |
Anusha |
Anucha |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 10:16 PM
Hi @Naveen Kokkirap ,
Please have a look at this community solution. https://www.servicenow.com/community/developer-forum/i-want-to-return-array-from-script-include-and-...
If it is helpful. Please mark it as correct.
Thanks,
Allu Gopal.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2024 03:24 AM
To create a table with the values returned by the script include, you can use the following code in your UI page:
<g:foreach var="value" items="${new ScriptInclude().get_Values()}">
</g:foreach><table>
<thead>
<tr>
<th>Student</th>
</tr>
</thead>
<tbody>
<tr>
<td>${value.student}</td>
</tr>
</tbody>
</table>
I hope this helps! Let me know if you have any questions.
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2024 04:32 AM - edited 02-23-2024 04:37 AM
Hi @Naveen Kokkirap
To fetch values from a Script Include and display them in tabular format on a UI page, you can follow these general steps. I'll provide a simplified example assuming you're working with a ServiceNow instance:
Define a Script Include with a function that retrieves the necessary data. For example:
var ScriptInCludeName = Class.create();
ScriptInCludeName.prototype = {
initialize: function () {},
getTableData: function () {
// Your logic to fetch and return data from the table
var grRec = new GlideRecord('your_table_name');
grRec.query();
var data = [];
while (grRec.next()) {
data.push({
field1: grRec.getValue('field1'),
field2: grRec.getValue('field2'),
// Add other fields as needed
});
}
return data;
},
};
Use the Script Include in UI Page:
- In your UI Page, you can use the Script Include to fetch data and display it in a tabular format. For example:
<html>
<body>
<table border="1">
<thead>
<tr>
<th>Field 1</th>
<th>Field 2</th>
</tr>
</thead>
<tbody>
<!-- Jelly loop to iterate over the data -->
<j:set var="tableData" value="${ScriptInCludeName().getTableData()}"/>
<j:forEach var="row" items="${tableData}">
<tr>
<td>${row.field1}</td>
<td>${row.field2}</td>
<!-- Add cells for other fields -->
</tr>
</j:forEach>
</tbody>
</table>
</body>
</html>
Remember to replace 'your_table_name', 'field1', 'field2', etc., with your actual table name and field names...and use these values in your Ui page Client Script.
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Warm Regards,
Subhashis r7
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2024 04:34 AM
You can utilize below script:
<table>
<thead>
<tr>
<th>Student</th>
</tr>
</thead>
<tbody>
<g:foreach var="value" items="${new ScriptInclude().get_Values()}">
<tr>
<td>${value.student}</td>
</tr>
</g:foreach>
</tbody>
</table>
Please mark it as "Accepted as Solution" & hit the helpful button, if it suffices your requirement.