The CreatorCon Call for Content is officially open! Get started here.

How to fetch values from script include to ui page in tabular format?

Naveen Kokkirap
Tera Contributor

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

 

 

5 REPLIES 5

Gopal Allu
Tera Expert

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.

Ademir Amaral1
Kilo Sage

Hi @Naveen Kokkirap 

 

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.

 

Subhashis Ratna
Tera Guru

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

Sohithanjan G
Kilo Sage

Hi @Naveen Kokkirap 

 

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. 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)