Build Email body at runtime

pratapdalai
Tera Contributor

Can you please suggest some option ,to build a grid  table dynamically for email body?.

6 REPLIES 6

Riya Verma
Kilo Sage
Kilo Sage

Hi @pratapdalai ,

 

Hope you are doing great.

 

To dynamically build a grid table for the email body in ServiceNow, you can create notification email script:

// Define an array of data to populate the grid table dynamically
var tableData = [
  { name: 'John', age: 30, city: 'New York' },
  { name: 'Jane', age: 28, city: 'London' },
  { name: 'Mark', age: 35, city: 'Paris' }
];

// Start building the HTML structure for the grid table
var htmlTable = '<table border="1"><tr>';

// Iterate over the keys of the first object in the data array to create table headers
for (var key in tableData[0]) {
  htmlTable += '<th>' + key + '</th>';
}

htmlTable += '</tr>';

// Iterate over each object in the data array to create table rows
for (var i = 0; i < tableData.length; i++) {
  htmlTable += '<tr>';

  // Iterate over the values of each object to create table cells
  for (var key in tableData[i]) {
    htmlTable += '<td>' + tableData[i][key] + '</td>';
  }

  htmlTable += '</tr>';
}

htmlTable += '</table>';

// Now, you can use the generated HTML table in your email body
var emailBody = 'Dear recipient,\n\nPlease find the grid table below:\n\n' + htmlTable + '\n\nBest regards,\nYour Name';

// Send the email with the dynamic grid table in the body
gs.eventQueue('send.email', current, 'sender@example.com', 'Subject', emailBody);
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Tony Chatfield1
Kilo Patron

Hi, normally you would use notification email script, basic format (untested)

template.print('<table>');

template.print('<tr>' + current.getDisplayVlaue('somefield') + '</tr>');

 

//or some glidequery to make a list from each result in the query

while(yourQuery.next()) {
template.print('<tr>' + yourQuery.somefield.toString() + '</tr>');

}

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

jaheerhattiwale
Mega Sage
Mega Sage

@pratapdalai I used the below code in email script for some other task but you can use this below script for your reference

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
 
    template.print('<table border="1px solid black">');
    template.print("<tr>");
    template.print("<th><left><b> Number </b></left></th>");
    template.print("<th><left><b> Short Description </b></left></th>");
    template.print("<th><left><b> Assigned To </b></left></th>");
    template.print("<th><left><b> Severity </b></left></th>");
    template.print("<th><left><b> Opened Date </b></left></th>");
    template.print("</tr>");
 
    var list = new GlideRecord('task');
    list.orderBy('priority');
    list.addEncodedQuery('assigned_to=' + current.sys_id.toString());
    list.query();
 
    var allTasksObject = [];
    while (list.next()) {
        var abc = {};
        abc.number = list.number.toString();
        abc.sd = list.short_description.toString();
        abc.assigned = list.assigned_to.getDisplayValue();
        abc.pr = list.priority.toString();
        abc.od = list.opened_at.toString();
        abc.sys_id = list.getUniqueValue();
abc.className = list.getRecordClassName();
        allTasksObject.push(abc);
    }
 
    for (var i = 0; i < allTasksObject.length; i++) {
        template.print("<tr>");
        template.print("<td><left><b><a href='/"+allTasksObject[i].className+".do?sys_id=" + allTasksObject[i].sys_id + "'>" + allTasksObject[i].number + "</a></b></left></td>");
        template.print("<td><left><b>" + allTasksObject[i].sd + "</b></left></td>");
        template.print("<td><left><b>" + allTasksObject[i].assigned + " </b></left></td>");
        template.print("<td><left><b>" + allTasksObject[i].pr + " </b></left></td>");
        template.print("<td><left><b>" + allTasksObject[i].od + " </b></left></td>");
        template.print("</tr>");
    }
    template.print('</table>');
 
})(current, template, email, email_action, event);
 
Please mark as correct answer if this has helped you.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

I have built the Flow Designer ,which get the data from different tables. The final step of the flow designer is to send the notification email.The email body should have dynamic  grid table based number of records.

Please suggest