Sagar Pagar
Tera Patron

Introduction:

I have received a business requirement, that can we add the inline report (table) instead of the excel attachments. For this, I search and start working on it, so I had few issues while working on this requirement. Hence, I have started to write this article. I think it will help community users to work on this type of requirement in the future.

Create a scheduled report which will trigger based on conditions and it will include the inline table as per business need.

 

Use cases:

  1. Create a scheduled report which will trigger based on some conditions and have reported with the inline-table format in the mail.
  2. Create a scheduled report with an inline table to send the assigned to users and his/her managers for pending approval of requests.

 

Procedure:

Step:1 Create a report and schedule it as per the required time.

Example- Create a report for example- P1/ P2 Incidents in a week.

find_real_file.png

 

Step:2 Scheduled the same report as per business need.

 find_real_file.png

 

Step:3 Create a Notification email script to add report contents(records) into inline table instead of the attachments.

 find_real_file.png

 

 

 

Here is the email scripts:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
										   /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
										   /* Optional GlideRecord */ event) {

	p1p2Incidents();

	function p1p2Incidents(){

		var inc = new GlideRecord(current.report.table);
		inc.addEncodedQuery(current.report.filter);
		inc.query();

		if (inc.hasNext()) {
			template.print('<br />');	
			template.print('<table style="height: 23px;" width="100%"><tbody><tr><td style="background-color: #0e5399;"><font color="#FFFFFF"><strong>P1/P2 Incidents:</strong></font></td></tr></tbody></table>');
			var tbl = '<table style="height: 293px;" width="100%"><tbody>';
			//template.print(tbl);

			var tab = '<table style="width: 100%; border-collapse : collapse; " border = "2" cellpadding = "10"> <tbody>' + '<tr>' +

				'<td style="background-color: #aeaeae;">' + '<b>Number</b>'  + '</td>' +
				'<td style="background-color: #aeaeae;">' + '<b>Opened</b>'  + '</td>' +


				'<td style="background-color: #aeaeae;">' + '<b>Priority</b>' + '</td>' + 
				'<td style="background-color: #aeaeae;">' + '<b>Name</b>'    + '</td>' + 

				'<td style="background-color: #aeaeae;">' + '<b>Description</b>'   + '</td>' + 
				'<td style="background-color: #aeaeae;">' + '<b>Requested by</b>'   + '</td>' + 


				'<td style="background-color: #aeaeae;">' + '<b>State</b>'   + '</td>' + 
				'<td style="background-color: #aeaeae;">' + '<b>Configuration item</b>'   + '</td>' + 

				'<td style="background-color: #aeaeae;">' + '<b>Assignment group</b>'   + '</td>' + 
				'<td style="background-color: #aeaeae;">' + '<b>Assigned to</b>'   + '</td>' + 

				'</tr>';
			template.print(tab);
		}

		while(inc.next())
		{   

			var clsed = '<tr>' + 
				'<td>' + inc.number + '</td>' +
				'<td>' + inc.opened_at + '</td>' +  


				'<td>' + inc.priority.getDisplayValue() + '</td>' +
				'<td>' + inc.short_description + '</td>' + 

				'<td>' + inc.description + '</td>' +
				'<td>' + inc.caller_id.name + '</td>' +

				'<td>' + inc.state.getDisplayValue() + '</td>' + 
				'<td>' + inc.cmdb_ci.name + '</td>' +  

				'<td>' + inc.assignment_group.name + '</td>' +  
				'<td>' + inc.assigned_to.name + '</td>' +

				'</tr>';
			template.print(clsed);
		}

		var tab_end = '</tbody> </table>';
		template.print(tab_end);

	}
})(current, template, email, email_action, event);

This will add the inline table dynamically.

 

 

Step:4 Execute a scheduled report & check the mail logs.

 find_real_file.png

 

Step:5 Check the Received email:

 find_real_file.png

 

Conclusion:

I this way, we can add inline table in mail body with the attachments in the scheduled report. It help to improve the user experience and reduce the time to download & open the attachments to view the data.

 

Please do not forget to mark helpful and bookmark this article!!!!!

Thanks!
Sagar Pagar

 

Comments
JosephW1
Tera Guru

Cool, thanks for sharing this and all your scripts! I think it's a very good article. Marking it helpful.

emaccrossan1
Tera Contributor

Thank you for publishing this article Sagar! - This is exactly what I needed.

servicenow lath
Tera Contributor

@Sagar Pagar How to Add two Reports in the Single Email script Using Your coding Method?

emaccrossan1
Tera Contributor

Hello Sagar  --You assisted me greatly with this article last year!

Is it possible to alternate the row colours on an inline table?

I have attempted to create this colouring using several different methods but have not been successful

Any help you can provide is greatly appreciated.

 

<nth-child: "background: "#F2F2F2 ">

 

 

Sagar Pagar
Tera Patron

Hi @emaccrossan1,

 

Yes. It is possible to alternate the row colors on an inline table.

 

You need to define any temporary count variable. Inside while loop check that count is odd or event and based on it apply background colors to rows. At the end just increment the count.

 

Format: '<td style="background-color: #F2F2F2;" >' + objName.column_name + '</td>' +

 

Sample scripts:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
	/* Optional EmailOutbound */
	email, /* Optional GlideRecord */ email_action,
	/* Optional GlideRecord */
	event) {

	p1p2Incidents();

	function p1p2Incidents() {
		var count = 1;

		var inc = new GlideRecord(current.report.table);
		inc.addEncodedQuery(current.report.filter);
		inc.query();

		if (inc.hasNext()) {
			template.print('<br />');
			template.print('<table style="height: 23px;" width="100%"><tbody><tr><td style="background-color: #0e5399;"><font color="#FFFFFF"><strong>P1/P2 Incidents:</strong></font></td></tr></tbody></table>');
			var tbl = '<table style="height: 293px;" width="100%"><tbody>';
			//template.print(tbl);

			var headerCols = '<table style="width: 100%; border-collapse : collapse; " border = "2" cellpadding = "10"> <tbody>' + '<tr>' +

				'<td style="background-color: #aeaeae;">' + '<b>Number</b>' + '</td>' +
				'<td style="background-color: #aeaeae;">' + '<b>Opened</b>' + '</td>' +
				'<td style="background-color: #aeaeae;">' + '<b>Priority</b>' + '</td>' +
				'<td style="background-color: #aeaeae;">' + '<b>Name</b>' + '</td>' +
				'<td style="background-color: #aeaeae;">' + '<b>Requested by</b>' + '</td>' +
				'<td style="background-color: #aeaeae;">' + '<b>State</b>' + '</td>' +
				'<td style="background-color: #aeaeae;">' + '<b>Configuration item</b>' + '</td>' +
				'<td style="background-color: #aeaeae;">' + '<b>Assignment group</b>' + '</td>' +
				'<td style="background-color: #aeaeae;">' + '<b>Assigned to</b>' + '</td>' +
				'</tr>';
			template.print(headerCols);
		}
		while (inc.next()) {
			var tableRows;
			if (count % 2 == 0) {
				tableRows = '<tr>' +
					'<td style="background-color: #F2F2F2;" >' + inc.number + '</td>' +
					'<td style="background-color: #F2F2F2;" >' + inc.opened_at + '</td>' +
					'<td style="background-color: #F2F2F2;" >' + inc.priority.getDisplayValue() + '</td>' +
					'<td style="background-color: #F2F2F2;" >' + inc.short_description + '</td>' +
					'<td style="background-color: #F2F2F2;" >' + inc.caller_id.name + '</td>' +
					'<td style="background-color: #F2F2F2;" >' + inc.state.getDisplayValue() + '</td>' +
					'<td style="background-color: #F2F2F2;" >' + inc.cmdb_ci.name + '</td>' +
					'<td style="background-color: #F2F2F2;" >' + inc.assignment_group.name + '</td>' +
					'<td style="background-color: #F2F2F2;" >' + inc.assigned_to.name + '</td>' +
					'</tr>';
			} else {
				tableRows = '<tr>' +
					'<td>' + inc.number + '</td>' +
					'<td>' + inc.opened_at + '</td>' +
					'<td>' + inc.priority.getDisplayValue() + '</td>' +
					'<td>' + inc.short_description + '</td>' +
					'<td>' + inc.caller_id.name + '</td>' +
					'<td>' + inc.state.getDisplayValue() + '</td>' +
					'<td>' + inc.cmdb_ci.name + '</td>' +
					'<td>' + inc.assignment_group.name + '</td>' +
					'<td>' + inc.assigned_to.name + '</td>' +
					'</tr>';
			}
			template.print(tableRows);
			count++;
		}

		var tab_end = '</tbody> </table>';
		template.print(tab_end);
	}
})(current, template, email, email_action, event);

 

 

Thanks,

Sagar Pagar

emaccrossan1
Tera Contributor

This is great! Thank you so much for your help, Sagar!

I am very grateful for this!

Have a great day!

Eamonn

Community Alums
Not applicable

Thank you so much it's worked for me

Karin Duijnker1
Tera Contributor

Hi Sagar, is it possible to add a pivot table in the body of the mail body.
That would be a very nice option...

Mohamed_Hazik
Tera Contributor

Hi Sagar,

Is it possible to do without attachment it should contain image only in the email body

Brian Lancaster
Tera Sage

Thanks for this exactly wat was requested of me. Never even occurred to me to try a mail script in a scheduled report.

Version history
Last update:
‎04-26-2021 04:36 AM
Updated by: