Print Counter using XML code

Lokesh5
Tera Contributor

Hi,

 

I was trying to print counter of numbers in a table using JSP code in servicenow. However, the counter is not working as excepted, its throwing null values. Please find my script below and correct me.

Thanks in advance!
----------------------------------------------------------

<table class="relatedlist_table">
            <tr>
                <th style="background-color: #1f4e79; color: white; width: 5%;">OBS Number</th>
                <th style="background-color: #1f4e79; color: white; width: 40%;">Observations Title</th>
                <th style="background-color: #1f4e79; color: white; width: 15%;">Rating</th>
                <th style="background-color: #1f4e79; color: white; width: 20%;">Responsible Party</th>
                <th style="background-color: #1f4e79; color: white; width: 20%;">Action Plan Target Date</th>
            </tr>

            <j:if test="${jvar_issuesenddate.getRowCount() > 0}">
                <script>
                    var jsCounter = 1;
                </script>
                <j:while test="${jvar_issuesenddate.next()}">
                    <tr>
                       
                        <td style="width: 5%;">
                        <script>
                        document.writeln(jsCounter++);
                        </script>
                        </td>
                        <td>${jvar_issuesenddate.getValue('short_description')}</td>
                        <td>${jvar_issuesenddate.getValue('short_description')}</td>
                        <td>${jvar_issuesenddate.getDisplayValue('assigned_to')}</td>
                        <td>${jvar_issuesenddate.getValue('u_target_date')}</td>
                    </tr>
                </j:while>
            </j:if>
        </table>

Lokesh5_0-1730627218768.png

 

3 REPLIES 3

Abhay Kumar1
Giga Sage

@Lokesh5 It looks like the issue is that the counter jsCounter is being incremented using JavaScript within the JSP while loop. However, JavaScript runs client-side, while the JSP <j:while> loop is processed server-side. This mismatch can lead to unexpected behavior, as jsCounter will not increment as expected across server-side iterations.

To handle this correctly, you should maintain the counter using JSP Server side code.

Hope this will help you.

Lokesh5
Tera Contributor

Hi @Abhay Kumar1 ,

Thanks for the quick response. I have followed the way which you suggested and its working. However, the counter is returning in decimal values like 1.0, 2.0..... I have tried all the conversion menthods but no luck. Could you please check my below code and suggest the way to eliminate decimals.

 

 

 
    <g:evaluate object="true" var="jvar_issuesenddate">
    var issueId = [];
   
    var counter = 1;    
    var engagementId = current.getValue('sys_id');
    var controlTest = new GlideRecord('sn_audit_control_test');
    controlTest.addQuery('top_task', engagementId);
    controlTest.addQuery('issue', '!=', '');
    controlTest.query();
   
    while (controlTest.next()) {
       issueId.push(controlTest.issue + '');  
    }
    var issues = new GlideRecord('sn_grc_issue');
    issues.addQuery('sys_id', 'IN', issueId.join(',')).addOrCondition('sn_audit_engagement', engagementId);
    issues.orderBy('number');
    issues.query();
   
    issues;
</g:evaluate>

<div id="exportContent">
    <body>
        <table class="relatedlist_table">
    <tr>
        <th style="background-color: #1f4e79; color: white; width: 5%;">OBS Number</th>
        <th style="background-color: #1f4e79; color: white; width: 40%;">Observations Title</th>
        <th style="background-color: #1f4e79; color: white; width: 15%;">Rating</th>
        <th style="background-color: #1f4e79; color: white; width: 20%;">Responsible Party</th>
        <th style="background-color: #1f4e79; color: white; width: 20%;">Action Plan Target Date</th>
    </tr>

    <j:if test="${jvar_issuesenddate.getRowCount() > 0}">
       
        <j:while test="${jvar_issuesenddate.next()}">
            <tr>
                <td style="width: 5%;">
                    ${Math.floor(counter)}
                </td>
                <td>${jvar_issuesenddate.getValue('short_description')}</td>
                <td>${jvar_issuesenddate.getValue('rating')}</td>
                <td>${jvar_issuesenddate.getDisplayValue('assigned_to')}</td>
                <td>${jvar_issuesenddate.getValue('u_target_date')}</td>
            </tr>
            <g:evaluate>
            counter++
        </g:evaluate>
        </j:while>
    </j:if>
</table>

@Lokesh5 use parseInt to explicitly convert it so it will make sure output is shown as an integer.