- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-18-2025 04:20 AM
How can I script in Mail script and create a table according to the Dynamic data. For example, If I am getting three fields so according to that if only two field have data so create a table showing only two fields and in future If All the fields have data so create a table according to that.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-18-2025 05:19 AM
You can use the following Mail Script to construct an HTML table dynamically:
// Define the fields to include in the table
var fields = [
{ label: "Incident Number", value: current.number },
{ label: "Short Description", value: current.short_description },
{ label: "Assigned To", value: current.assigned_to.getDisplayValue() }
];
// Start building the HTML table
var tableHTML = "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 100%;'>";
tableHTML += "<tr><th>Field</th><th>Value</th></tr>";
// Loop through the fields and add only those with data
for (var i = 0; i < fields.length; i++) {
if (fields[i].value) { // Check if the field has a value
tableHTML += "<tr>";
tableHTML += "<td><b>" + fields[i].label + "</b></td>";
tableHTML += "<td>" + fields[i].value + "</td>";
tableHTML += "</tr>";
}
}
// Close the table
tableHTML += "</table>";
// Print the table in the email body
gs.print(tableHTML);
Example Outputs
Case 1: All Fields Have Data
Field Value
Incident Number | INC12345 |
Short Description | Network Issue |
Assigned To | John Doe |
Case 2: One Field is Missing (No Assigned To)
Field Value
Incident Number | INC12345 |
Short Description | Network Issue |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-18-2025 04:32 AM
Hi @Sachin bagyal ,
I think, you are referring the HTML <table> here, or database table.
If it's html <table> then you can use IF condition to check the dynamic data data and add the html code inside the template.print(). You can use all html tags inside.
-Thanks,
AshishKM
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-18-2025 04:33 AM
Yes, Ashish I am referring to HTML table but can you elaborate more on this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-18-2025 05:02 AM
please share your current email script so that we can help
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-18-2025 05:19 AM
You can use the following Mail Script to construct an HTML table dynamically:
// Define the fields to include in the table
var fields = [
{ label: "Incident Number", value: current.number },
{ label: "Short Description", value: current.short_description },
{ label: "Assigned To", value: current.assigned_to.getDisplayValue() }
];
// Start building the HTML table
var tableHTML = "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 100%;'>";
tableHTML += "<tr><th>Field</th><th>Value</th></tr>";
// Loop through the fields and add only those with data
for (var i = 0; i < fields.length; i++) {
if (fields[i].value) { // Check if the field has a value
tableHTML += "<tr>";
tableHTML += "<td><b>" + fields[i].label + "</b></td>";
tableHTML += "<td>" + fields[i].value + "</td>";
tableHTML += "</tr>";
}
}
// Close the table
tableHTML += "</table>";
// Print the table in the email body
gs.print(tableHTML);
Example Outputs
Case 1: All Fields Have Data
Field Value
Incident Number | INC12345 |
Short Description | Network Issue |
Assigned To | John Doe |
Case 2: One Field is Missing (No Assigned To)
Field Value
Incident Number | INC12345 |
Short Description | Network Issue |