How can I script in Mail script and create a table according to the Dynamic data.

Sachin bagyal
Tera Contributor

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.

1 ACCEPTED SOLUTION

sunil maddheshi
Tera Guru

@Sachin bagyal 

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 NumberINC12345
Short DescriptionNetwork Issue
Assigned ToJohn Doe

Case 2: One Field is Missing (No Assigned To)

Field Value

Incident NumberINC12345
Short DescriptionNetwork Issue

View solution in original post

4 REPLIES 4

AshishKM
Kilo Patron
Kilo Patron

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

Yes, Ashish I am referring to HTML table but can you elaborate more on this.

Ankur Bawiskar
Tera Patron
Tera Patron

@Sachin bagyal 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

sunil maddheshi
Tera Guru

@Sachin bagyal 

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 NumberINC12345
Short DescriptionNetwork Issue
Assigned ToJohn Doe

Case 2: One Field is Missing (No Assigned To)

Field Value

Incident NumberINC12345
Short DescriptionNetwork Issue