how to script rows and columns for records in an email script?

patricksmithlat
Mega Contributor

Hi community,

I'm trying to create an email script that takes records received from a GlideRecord and shows a couple fields from the record in an email.  I can get the info, but formatting script so it looks right is proving to be challenging.  Here's my script:

 

find_real_file.png

 

and here's what it looks like in the email

find_real_file.png

 

what i want is a nice table look with columns that have "Number", "State", and "Summary" going horizontal, with the appropriate values shown beneath...kind of like a spreadsheet view, or even a typical ServiceNow list view.  

any help would be great...thanks!

1 ACCEPTED SOLUTION

Archana Reddy2
Tera Guru

Sure. Here it is,

find_real_file.png

 

find_real_file.png

Thanks

View solution in original post

9 REPLIES 9

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You'd need to structure it a little differently to print the html table. Here's some psuedo code:

 

<table><tr><td>Number</td><td>State</td><td>Summary</td></tr>

open while loop

tr

print td task number /td

print td etc /td

etc.

/tr

end while loop

/table

Erik Stolberg
Tera Guru

You're looking to implement HTML Tables: https://www.w3schools.com/html/html_tables.asp

You will want to create one table with a table header and a couple rows. Here's the HTML for that:

<table>
  <tr>
    <th>Number</th>
    <th>State</th>
    <th>Summary</th>
  </tr>
  <tr>
    <td>gr.task.number</td>
    <td>gr.task.state</td>
    <td>gr.task.short_description</td>
  </tr>
</table>

You then need to incorporate that into your template.print() statements like you're already doing.

If you want to add borders and row highlighting, check out the reference link for some examples. You may also want to look into making the table width 100% or some set value to make it cleaner. I would recommend looking at some of the ways ServiceNow implements this in baseline code as well (suiteCompletion.email notification email script for example).

Archana Reddy2
Tera Guru

Hi,

Though my reply is a little messy, I believe this is what you are looking for. You may need to adjust the styles.

For Table Heading:

template.print("<style>table{width:100%;border-collapse:collapse;} th{border:1px solid black;font-family:Arial;font-size:11pt;padding:10px;}, td</style><table><tr><th width=20%>Number</th><th width=20%>State</th><th width=60%>Summary</th></tr></table>");

For Table Rows and Data:

To be written after writing GlideRecord.

 template.print("<html><style>table{width:100%;} td{border:1px solid black;padding:10px;font-family:Arial;font-size:10pt;border-top:0px;}</style><table><tr><td width=20%>gr.task.number</td><td width=20%>gr.task.u_state</td><td width=60%>gr.task.short_description</td></tr></table></html>");

Hope this helps.

Mark the answer as Correct/Helpful based on its impact.

Thanks

this was quickly helpful and helped me smoothly chowk out table , still headings and data needed some alignment as you mentioned to adjust so i added this piece 


table{width:100%;border-collapse:collapse;table-layout: fixed;}

and 

td{border:1px solid black;font-family:Arial;font-size:11pt;padding:10px;word-wrap: break-word;}

 

 

this fixed its alignment also

once again thanks Archana for quick help