Email Script - Create html table for the users listed in watchlist

Bindhu1
Tera Contributor

Hi,

I have created an email script to fetch the user's details added in watchlist of catalog item.

I am trying to display the result in a table structure. Kindly help me to do the same

expected result:

Bindhu1_0-1671001055792.png

result of below code

Bindhu1_1-1671001121950.png

cope snippet of mail script

Bindhu1_3-1671001377392.png

 

// Get the list of users in the watchlist
var watchlist = current.watch_list.split(',');

// Create the email subject and body
var subject = current.number + ":" + " Watchlist Members Notification";
email.setSubject(subject);

var body = "This is a courtesy note to avoid CI incidents (reword). <br/><br/>";
template.print(body);

for (var i = 0; i < watchlist.length; i++) {
var user = new GlideRecord('sys_user');
user.get(watchlist[i]);

template.print('<style>');
template.print('table {border-collapse: collapse;border: 2px solid #ddd;text-align: left;}');
template.print('th, td {padding: 3px;border: 1px solid #ddd}');
template.print('tr:hover{background-color:#f5f5f5}');
template.print('</style>');
template.print('<table>');
template.print('<tr>');

template.print('<th>Name</th>');
template.print('<th>Email</th>');
template.print('<th>Location</th>');
template.print('<th>Title</th>');
template.print('</tr>');

template.print("<tr>");
//template.print("<tr ng-repeat=user in >");
template.print("<td>" + user.name + "</td>");
template.print("<td>" + user.email + "</td>");
template.print("<td>" + user.location.name + "</td>");
template.print("<td>" + user.title + "</td>");
template.print("</tr>");
template.print('</table>');
}

 

Thanks

 

 

1 ACCEPTED SOLUTION

prashanth2
Kilo Guru

Hey , try below code,

var watchlist = current.watch_list.split(',');

// Create the email subject and body
var subject = current.number + ":" + " Watchlist Members Notification";
email.setSubject(subject);

var body = "This is a courtesy note to avoid CI incidents (reword). <br/><br/>";
    template.print(body);
    template.print('<style>');
    template.print('table {border-collapse: collapse;border: 2px solid #ddd;text-align: left;}');
    template.print('th, td {padding: 3px;border: 1px solid #ddd}');
    template.print('tr:hover{background-color:#f5f5f5}');
    template.print('</style>');
    template.print('<table>');
    template.print('<tr>');

    template.print('<th>Name</th>');
    template.print('<th>Email</th>');
    template.print('<th>Location</th>');
    template.print('<th>Title</th>');
    template.print('</tr>');

    for (var i = 0; i < watchlist.length; i++) {
        var user = new GlideRecord('sys_user');
        user.get(watchlist[i]);




        template.print("<tr>");
        //template.print("<tr ng-repeat=user in >");
        template.print("<td>" + user.name + "</td>");
        template.print("<td>" + user.email + "</td>");
        template.print("<td>" + user.location.name + "</td>");
        template.print("<td>" + user.title + "</td>");
        template.print("</tr>");

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

 

If this helps, please mark the answer correct/helpful.

 

Regards,

Prashanth

View solution in original post

2 REPLIES 2

prashanth2
Kilo Guru

Hey , try below code,

var watchlist = current.watch_list.split(',');

// Create the email subject and body
var subject = current.number + ":" + " Watchlist Members Notification";
email.setSubject(subject);

var body = "This is a courtesy note to avoid CI incidents (reword). <br/><br/>";
    template.print(body);
    template.print('<style>');
    template.print('table {border-collapse: collapse;border: 2px solid #ddd;text-align: left;}');
    template.print('th, td {padding: 3px;border: 1px solid #ddd}');
    template.print('tr:hover{background-color:#f5f5f5}');
    template.print('</style>');
    template.print('<table>');
    template.print('<tr>');

    template.print('<th>Name</th>');
    template.print('<th>Email</th>');
    template.print('<th>Location</th>');
    template.print('<th>Title</th>');
    template.print('</tr>');

    for (var i = 0; i < watchlist.length; i++) {
        var user = new GlideRecord('sys_user');
        user.get(watchlist[i]);




        template.print("<tr>");
        //template.print("<tr ng-repeat=user in >");
        template.print("<td>" + user.name + "</td>");
        template.print("<td>" + user.email + "</td>");
        template.print("<td>" + user.location.name + "</td>");
        template.print("<td>" + user.title + "</td>");
        template.print("</tr>");

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

 

If this helps, please mark the answer correct/helpful.

 

Regards,

Prashanth

Sagar Pagar
Tera Patron

Hi @Bindhu1,

Your table header should be out side of loop.

 

Try this updated scripts -

 Get the list of users in the watchlist
 var watchlist = current.watch_list.split(',');

 // Create the email subject and body
 var subject = current.number + ":" + " Watchlist Members Notification";
 email.setSubject(subject);

 var body = "This is a courtesy note to avoid CI incidents (reword). <br/><br/>";
 template.print(body);

 template.print('<style>');
 template.print('table {border-collapse: collapse;border: 2px solid #ddd;text-align: left;}');
 template.print('th, td {padding: 3px;border: 1px solid #ddd}');
 template.print('tr:hover{background-color:#f5f5f5}');
 template.print('</style>');
 template.print('<table>');
 template.print('<tr>');

 template.print('<th>Name</th>');
 template.print('<th>Email</th>');
 template.print('<th>Location</th>');
 template.print('<th>Title</th>');
 template.print('</tr>');
 for (var i = 0; i < watchlist.length; i++) {
 	var user = new GlideRecord('sys_user');
 	user.get(watchlist[i]);

 	template.print("<tr>");
 	//template.print("<tr ng-repeat=user in >");
 	template.print("<td>" + user.name + "</td>");
 	template.print("<td>" + user.email + "</td>");
 	template.print("<td>" + user.location.name + "</td>");
 	template.print("<td>" + user.title + "</td>");
 	template.print("</tr>");
 	template.print('</table>');
 }

 

Thanks,

Sagar Pagar

The world works with ServiceNow