Build Email body at runtime
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2023 12:47 AM
Can you please suggest some option ,to build a grid table dynamically for email body?.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2023 01:52 AM
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);
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2023 01:58 AM
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>');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2023 02:01 AM
@pratapdalai I used the below code in email script for some other task but you can use this below script for your reference
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2023 09:08 PM
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