Including "Email Scripts" in a template or notification

Makosko
Tera Expert

Guys,

 

has anybody managed to figure out how to include an email script in a template ?

 

Cheers

Maros

17 REPLIES 17

cheers for this, looking good..



I will send you mine in a short while...


Sorry for the delay!



as promised, I am including my class:



var UserServiceNowDigest = Class.create();
UserServiceNowDigest.getDate = function() {
      var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
              months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
      var gdt = new GlideDateTime();


      return (days[gdt.getDayOfWeekLocalTime() - 1] + " " + gdt.getDayOfMonthLocalTime() + " " + months[gdt.getMonthLocalTime() - 1] + " " + gdt.getYearLocalTime());
}


UserServiceNowDigest.getAudience = function() {
      var gr = new GlideRecord("sys_user")
              gr.addActiveQuery();
              gr.addJoinQuery("sys_user_has_role","sys_id","user");
              gr.query();
      return gr;
}


UserServiceNowDigest.prototype = {
      root_url: gs.getProperty('glide.servlet.uri'),


      cell_across_style: 'padding: 0 0 5px 0; border-bottom: 1px solid #dddddd; text-align: left; font-weight: bold; font-size: 13px;',
      cell_class_style: 'padding-top: 8px; font-weight: bold;',
      cell_row_style: 'padding-right: 10px; white-space:nowrap;',
      view_all_style: 'padding:8px 0; text-align:center; border-top:1px solid #ddd;',


      rp_project_url: 'com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=db685632208c7c00875f750ec43c55e5',
      rp_incident_url: 'com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=82febb7b55df5cc496348ccfdc7a89e7',
      rp_request_url: 'com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=cdfdf77b55df5cc496348ccfdc7a89aa',
      unsubscribe_url: ('unsubscribe.do?sysparm_notification=' + email_action.sys_id),

      tmp_table_open: '<table style="width: 100%; border:none; font-family: arial, helvetica, sans-serif; font-size:12px;">',
      tmp_table_close: '</table>',


      mailto: 'mailto:informa@service-now.com?',


      initialize: function(gSource) {
              this.tmpl                   = MicroTemplateEngine;
              this.gCurrent           = gSource;
              this.arrayUtils       = new ArrayUtil();
              this.show_limit       = 10;


              this.policies = ["20759702657a6140407ecc2c2dcc589b", "a24b8fca65b66140407ecc2c2dcc5867"];
      },


      render: function() {
              this._renderPersonalInfo();
              template.print("<br />");
              this._renderApprovals();
              template.print("<br />");
              this._renderOpenTickets();
              template.print("<br />");
              this._renderWatchedTickets();
              template.print("<br />");
              this._renderAssets();
              template.print("<br />");
              this._renderPolicies();
              template.print("<br /><hr/>");
              this._renderExtraLinks();
      },



      // Private Methods
      _renderPersonalInfo: function() {
              var updateAccountLink = this.root_url +'u_itp_update_account.do?sysparm_stack=u_itp_update_account_list.do&sys_id=-1';


              template.print(this.tmp_table_open);
              template.print(this.tmpl(gs.getMessage("html.record_cell_across"), {
                      value: "My Details - " + this.tmpl(gs.getMessage('html.link_standard'),{value: 'Click to update', link: updateAccountLink }),
                      style: this.cell_across_style + "padding-top:10px;"
              }));


              template.print('<tr><td style="text-align: left; padding: 15px 0;">');
              template.print('<div><b>Name: </b>${name}</div>');
              template.print('<div><b>Manager: </b>${manager}</div>');
              template.print('<div><b>Location: </b>${location}</div>');
              template.print('<div><b>Division: </b>${u_division}</div>');
              template.print('<div><b>Company: </b>${company}</div>');
              template.print('<div><b>Email: </b>${email}</div>');
              template.print('<div><b>Telephone: </b>${phone}</div>');
              template.print('</td></tr>');


              template.print(this.tmp_table_close);  
      },


      _renderPolicies: function() {
              template.print(this.tmp_table_open);
              template.print(this.tmpl(gs.getMessage("html.record_cell_across"), {
                      value: "My Policies",
                      style: this.cell_across_style
              }));


              var self = this;
              this.arrayUtils.union(PolicyInstanceHelper.getPoliciesByUser(event.parm1), this.policies).forEach(function(policyId) {
                        var gr = new GlideRecord("u_policy2");
                        if (gr.get(policyId)) {


                                var second = "";
                                if(!gr.u_record_producer.nil()) {
                                        var linkText = PolicyInstanceHelper.hasYesSubmission(gr.getUniqueValue(), event.parm1) ? "View" : "Take",
                                                link         = self.root_url + GlideappCatalogURLGenerator.getItemBaseURLFromGR(gr.u_record_producer.getRefRecord());
                                                second     = self.tmpl(gs.getMessage('html.link_standard'), {value: linkText, link: link})
                                }
                                template.print(self.tmpl(gs.getMessage("html.tr2cells_with_link"), {
                                          first: gr.getDisplayValue(),
                                          second: second,
                                          style: self.cell_row_style
                                }));
                        }
              })
              template.print(this.tmp_table_close);
      },


      _renderOpenTickets: function() {
              var gr = new GlideRecord("task");
                      gr.addEncodedQuery("u_customer=" + event.parm1 + "^state!=3^state!=38^state!=60");
                      gr.orderBy("sys_class_name");
                      gr.orderByDesc("sys_created_on");
                      gr.orderByDesc("sys_updated_on");
                      gr.setLimit(this.show_limit);
                      gr.query();


              template.print(this.tmp_table_open);
              template.print(this.tmpl(gs.getMessage("html.record_cell_across"), {
                      value: "My Open Tickets",
                      style: this.cell_across_style
              }));


              while(gr.next()) {
                      template.print(this.tmpl(gs.getMessage("html.tr3cells_with_link"), {
                            first:   gr.getDisplayValue(),
                            second: gr.getClassDisplayValue(),
                            third:   gr.short_description.toString(),
                            link:   (this.root_url + gr.getLink()),
                            style: this.cell_row_style
                      }));
              }
              this._showViewMoreLink(gr);
              template.print(this.tmp_table_close);
      },


      _renderWatchedTickets: function() {
              var gr = new GlideRecord("task");
                      gr.addEncodedQuery("watch_listLIKE" + event.parm1 + "^state!=3^state!=38^state!=60");
                      gr.orderBy("sys_class_name");
                      gr.orderByDesc("sys_created_on");
                      gr.orderByDesc("sys_updated_on");
                      gr.setLimit(this.show_limit);
                      gr.query();


              template.print(this.tmp_table_open);
              template.print(this.tmpl(gs.getMessage("html.record_cell_across"), {
                      value: "My Watched Tickets",
                      style: this.cell_across_style
              }));


              while(gr.next()) {
                      template.print(this.tmpl(gs.getMessage("html.tr3cells_with_link"), {
                            first: gr.getDisplayValue(),
                            second: gr.getClassDisplayValue(),
                            third:   gr.short_description.toString(),
                            link: (this.root_url + gr.getLink()),
                            style: this.cell_row_style
                      }));
              }
              this._showViewMoreLink(gr);
              template.print(this.tmp_table_close);                                  
      },


      _renderAssets: function() {
                var gr = new GlideRecord("cmdb_ci");
                        gr.addEncodedQuery("assigned_to=" + event.parm1 + "^ORowned_by=" + event.parm1 + "^install_statusNOT IN9");
                        gr.setLimit(this.show_limit);
                        gr.query();


                template.print(this.tmp_table_open);
                template.print(this.tmpl(gs.getMessage("html.record_cell_across"), {
                      value: "My Assets",
                      style: this.cell_across_style
                }));


                while(gr.next()) {
                      template.print(this.tmpl(gs.getMessage("html.tr2cells_with_link"), {
                            first: gr.getDisplayValue(),
                            second: gr.model_id.getDisplayValue(),
                            link: (this.root_url + gr.getLink()),
                            style: this.cell_row_style
                      }));
                }
                this._showViewMoreLink(gr);
                template.print(this.tmp_table_close);
      },


      _renderApprovals: function() {
                var gr = new GlideRecord("sysapproval_approver");
                        gr.addEncodedQuery("approver="+event.parm1 + "^stateINrequested");
                        gr.setLimit(this.show_limit);
                        gr.orderByDesc("sys_created_on");
                        gr.orderByDesc("sys_updated_on");
                        gr.query();


                template.print(this.tmp_table_open);
                template.print(this.tmpl(gs.getMessage("html.record_cell_across"), {
                      value: "My Approvals",
                      style: this.cell_across_style
                }));


                while(gr.next()) {                    


                      template.print(this.tmpl(gs.getMessage("html.tr3cells_with_link"), {
                            first: gr.sysapproval.getDisplayValue(),
                            second: gr.sysapproval.short_description.toString(),
                            third: this._getApprovalLinks(gr),
                            link: (this.root_url + gr.getLink()),
                            style: this.cell_row_style
                      }));
                }
                this._showViewMoreLink(gr);
                template.print(this.tmp_table_close);                    
      },


      _renderExtraLinks: function() {
              var msg_incident = 'Something broken or not working the way you expect ?',
                      msg_request   = 'Wish to request something new or you need to do your job?',
                      msg_project   = 'Wish to request a project?',
                      msg_unsubscribe = 'Don`t want to receive this weekly digest anymore ?';


              template.print(this.tmp_table_open);              
              template.print(this.tmpl(gs.getMessage("html.tr2cells_with_link"),{first: msg_incident, second: this.tmpl(gs.getMessage('html.link_standard'), {value: 'Click Here', link: (this.root_url + this.rp_incident_url)})}));
              template.print(this.tmpl(gs.getMessage("html.tr2cells_with_link"),{first: msg_request, second: this.tmpl(gs.getMessage('html.link_standard'), {value: 'Click Here', link: (this.root_url + this.rp_request_url)})}));
              template.print(this.tmpl(gs.getMessage("html.tr2cells_with_link"),{first: msg_project, second: this.tmpl(gs.getMessage('html.link_standard'), {value: 'Click Here', link: (this.root_url + this.rp_project_url)})}));
              template.print(this.tmpl(gs.getMessage("html.tr2cells_with_link"),{first: msg_unsubscribe, second: this.tmpl(gs.getMessage('html.link_standard'), {value: 'Click here to unsubscribe', link: (this.root_url + this.unsubscribe_url)})}));
              template.print(this.tmp_table_close);
      },


      _showViewMoreLink: function(gr) {
                if(this._getCount(gr) > this.show_limit) {


                      var linkText = "View All " + gr.getLabel() + " records"
                      var link = this.root_url + gr.getRecordClassName() + '_list.do?sysparm_query='+gr.getEncodedQuery();


                      template.print(this.tmpl(gs.getMessage("html.record_cell_across"), {
                                value:   this.tmpl(gs.getMessage('html.link_standard'), {value: linkText, link: link}),
                                style:   this.view_all_style                              
                      }));
                }
      },


      _getCount: function(gr) {
              var answer = 0;
              var counter = new GlideAggregate(gr.getRecordClassName());
                      counter.addAggregate('COUNT');
                      counter.addEncodedQuery(gr.getEncodedQuery());
                      counter.query();
              if (counter.next()) {
                      answer = counter.getAggregate('COUNT');
              }
              return answer;
      },


      _getApprovalLinks: function(gApproval) {


                var answer = '', self = this;
                var gr = new GlideRecord("sys_email");
                        gr.addQuery("instance", gApproval.getUniqueValue());
                        gr.addQuery("mailbox","IN","sent,outbox");
                        gr.orderBy("sys_created_on");      
                        gr.query();


                if(gr.next()) {


                      gr.body.toString().replace(/Ref:([^\<]+)/gm, function(all, p1){


                            answer = self.tmpl(gs.getMessage('html.link_standard'), {value: 'Approve', link: (self.mailto + "subject="+ encodeURIComponent(("Re:"+ gApproval.sysapproval.getDisplayValue() + " - approve")) + "&amp;body=" + encodeURIComponent(all))}) + " | " + self.tmpl(gs.getMessage('html.link_standard'), {value: 'Reject', link: (self.mailto + "subject="+ encodeURIComponent(("Re:"+ gApproval.sysapproval.getDisplayValue() + " - reject")) + "&amp;body=" + encodeURIComponent(all))})    
                                               
                      })
                }
                return answer;
      },


      type: 'UserServiceNowDigest'
}


marcguy
ServiceNow Employee
ServiceNow Employee

wow, you've put some effort into that, awesome job!


Cookie.png


manish_vinayak
Kilo Expert

Hi Maros,



You should be able to use email scripts in email templates by using <mail_script> tag, i.e,



<mail_script>


//Your script goes here


</mail_script>



But if you are using it on eureka version, you'd have to create a separate email script and link that email notification script to that template by calling the mail script in the message:



${mail_script:script name}



Hope that helps.


Please let me know if more information is needed.



Regards,


Manish