For loop in Jelly script

Laxmi Prasanna1
Kilo Contributor

I need to generate a report to check the quality of cmdb by calculating attribute completeness like how many fields are filled and how many are empty.I tried implementing with the following script.

<?xml version="1.0" encoding="utf-8" ?>

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

  <g:evaluate var="jvar_text" object="true" >

  var gr=new GlideRecord('cmdb_ci_netgear');

  gr.addQuery('name','!=',null);

                                                              gr.query();

  var tc=gr.getRowCount();

  var fNames = ["name","model_id","asset_tag","manufacturer"];

  var l=fNames.length;

  var count = [];

  for(var i=0; i != l;i++){

        var j=0;

  var gr1=new GlideRecord('cmdb_ci_netgear');

  gr1.addQuery('fNames[i]','!=',null);

  gr1.query();

  count[j]=gr1.getRowCount();

  j++;

  }

  </g:evaluate>

<table style="width:100%">

  <tr style="font-weight:bold">

                                                              <th>FieldName</th>

                                                              <th>Count</th>

                                                              <th>Percent Complete</th>                                                              

                                              </tr>

  <tr>

                                                      <td>Name</td>

                                                      <td>${count[0]}</td>

  <td>${np}%</td>

                                              </tr>

  <tr>

                                                      <td>Model Id</td>                    

                                                      <td>${model_id}</td>  

                                                          <td>${count[1]}</td>                                                                                                      

                                              </tr>

</j:jelly>

Like this I have to do for 15 fields in a class.How can I generalize this code.Please help me out with this.

3 REPLIES 3

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Jelly actually has a forEach tag you can use to create repetitive html. This is a good blog post introducing the concept.



Jelly: Over and Over Again...


Thank you Brad.I am able to get the values from array using jelly forEach tag.How can I query the class for multiple fields and get the count for each filed and store it in a table.This is my actual requirement.I tried with the following code.


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


  <g:evaluate var="jvar_text" object="true" >  


             


  var fields = ['name', 'model_id', 'asset_tag', 'manufacturer'];


     


  </g:evaluate>



  <j:forEach var="jvar_field" items="${fields}">


  <g:evaluate jelly="true">


  var j=0;


  var count = [ ];


                      var gr1 =new GlideRecord('cmdb_ci_netgear');


  gr1.addQuery('${jvar_field}','!=',null);


                      gr1.query();


  count[j]=gr1.getRowCount();


  j++;


              </g:evaluate>



  </j:forEach>



  <j:forEach var="jvar_count" items="${count}">


  ${jvar_count}


  </j:forEach>






</j:jelly>



My output should look like


find_real_file.png


I need to calculate the filed count like how many name field,model_id filed etc are filled and need to get the % of complete for the same.It should look like a table.I am able to generate the same table using my script which is so lengthy.I need to query it multiple times for each and every field.So need to generalize it using for loop.Please suggest any other possible solutions to make it out.Thanks in advance


I think you would write your forEach, then inside the forEach you would call a script include which grabs the count and percent complete based on the field name you send. Then still inside the forEach you would build the table row with the three values.