Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need help on creating dynamic table in email script

HarikaG
Giga Contributor

Hi All,

I am getting below response from a script.

 

{"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"}]}

 

Now I would like to print the data in to a table and need send as notification.

 

 

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

    // Add your code here
  
    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>Alert 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);

I am trying with above script. But it is coming as single table. I would like to print historic and recent incidents line by line

HarikaG_1-1709906462084.png

 

How to print this data as dynamic table? Please assist me on this.

 

2 REPLIES 2

Tony Chatfield1
Kilo Patron

Hi, unfortunately your post is a little unclear as to outcome but if you want a single row in table for all 'Recent Incidents' then based on your code I think it would look something like

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

 

if you want an individual row per entry them you would need to loop through your payload creating HTML per entry, possibly like this

    for (i = 0; i < recentIncidents.length; i++) {
		template.print( "<tr>" );
		template.print( "<td><left><b>" + "Recent Incident" + "</b></left></td>" );
		template.print( "<td><left><b>" + recentIncidents[i]+  "</b></left></td>" );
		template.print( "</tr>" );
    }

 

Amit Verma
Kilo Patron
Kilo Patron

Hi @HarikaG 

 

Can you please let us know how you want the representation to be ? Your requirement is not very clear. If you could share some sample representation, may be we can help.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.