logo

NJP

Scheduling a report with an inline table in mail body with the attachments

Import · Apr 26, 2021 · article

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.

image

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

image

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

image

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.

image

Step:5 Check the Received email:

image

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

View original source

https://www.servicenow.com/community/platform-analytics-articles/scheduling-a-report-with-an-inline-table-in-mail-body-with-the/ta-p/2300611