Need print json data into dynamic table

HarikaG
Giga Contributor

Hi all,

 

I am getting below response from a script as json array. I would like to print that in dynamic table.

 

{"categoryKey":"Business Intelligence Sim|Enrollment Sim|Other","recentIncidents":[{"number":"INC4449511"},{"number":"INC4449512"}],"historicIncidents":[{"number":"INC4306467"},{"number":"INC4347468"},{"number":"INC4359174"},{"number":"INC4380294"},{"number":"INC4422274"},{"number":"INC4449511"},{"number":"INC4449512"}]}

 

I am trying with below script.

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var category = payload.categoryKey;
    var recentIncidentNum = [];
    var recentIncidents = payload.recentIncidents;
    for (i = 0; i < recentIncidents.length; i++) {
        recentIncidentNum.push(recentIncidents[i].number.toString());
    }
    var historicIncidentNum = [];
    var historicIncident = payload.historicIncidents;
    for (j = 0; j < historicIncident.length; j++) {
        historicIncidentNum.push(historicIncident[j].number.toString());
    }
 	
	template.print('<table border="1px solid black">');

template.print( "<tr bgcolor='#ddd' align='center'>" );
template.print("<td style='text-align:center' colspan='3'><strong>Similar Incidents</strong></td>");
template.print( "</tr>" );

template.print( "<tr>" );
template.print( "<td><left><b>" + "Recent Incidents" + "</b></left></td>" );
template.print( "<td><left><b> "+"Historic Incidents"+" </b></left></td>" );
template.print( "<td><left><b> "+"Category"+" </b></left></td>" );
template.print( "</tr>" );

template.print( "<tr>" );
template.print( "<td><left>" + recentIncidentNum + "</left></td>" );
template.print( "<td><left>" +historicIncidentNum+ "</b></left></td>" );
template.print( "<td><left>" +category +"</left></td>" );

template.print( "</tr>" );
template.print('</table>');

})(current, template, email, email_action, event);

 

It is getting printed as below.

 

HarikaG_0-1709907630486.png

 

Would like to print Recent Incidents and historic incidents line by line dynamically based on number of incidents.

Please assist me on this.

1 REPLY 1

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @HarikaG , 

To print the recent incidents and historic incidents line by line in rows, you need to iterate through each incident and print it individually. Here's the modified script to achieve that:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var payload = JSON.parse(current.body); // Assuming 'payload' contains your JSON data

    var category = payload.categoryKey;
    var recentIncidents = payload.recentIncidents;
    var historicIncidents = payload.historicIncidents;

    template.print('<table border="1px solid black">');

    template.print("<tr bgcolor='#ddd' align='center'>");
    template.print("<td colspan='3'><strong>Similar Incidents</strong></td>");
    template.print("</tr>");

    template.print("<tr>");
    template.print("<td><strong>Recent Incidents</strong></td>");
    template.print("<td><strong>Historic Incidents</strong></td>");
    template.print("<td><strong>Category</strong></td>");
    template.print("</tr>");

    var maxCount = Math.max(recentIncidents.length, historicIncidents.length);

    for (var i = 0; i < maxCount; i++) {
        template.print("<tr>");
        template.print("<td>" + (recentIncidents[i] ? recentIncidents[i].number : "") + "</td>");
        template.print("<td>" + (historicIncidents[i] ? historicIncidents[i].number : "") + "</td>");
        template.print("<td>" + category + "</td>");
        template.print("</tr>");
    }

    template.print('</table>');

})(current, template, email, email_action, event);

This script iterates through both recent and historic incidents simultaneously and prints them line by line. If one of the arrays is shorter than the other, it prints empty cells for that row. Make sure to adjust it according to your exact needs and JSON structure.

🙂

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..:)