Skip to main content

ServiceNow Pull a pdf attachment into an email containing change info (pdf generator)

Pre-requisite

the PDF generator needs to be installed in the instance

 

this can be installed from plugins I believe:


Business rule

 

 

(function executeRule(current, previous /*null when async*/) { var chg = new GlideRecord(current.target_table); chg.get(current.instance); if (chg.isValidRecord()) { var br = '<br/><br/>'; // Line breaks var html_content = '<h3>' + 'Number: ' + chg.number + br + 'Type: ' + chg.getDisplayValue('type') + br + 'State: ' + chg.getDisplayValue('state') + br + 'Service Offering: ' + chg.service_offering.name + br + 'Category: ' + chg.category + br + 'Priority: ' + chg.getDisplayValue('priority') + br + 'Outage Start Time: ' + chg.u_outage_start_date_time + br + 'Outage End Time: ' + chg.u_outage_end_date_time + br + '</h3>'; file_name = current.subject + '.pdf'; target_record_table = current.getTableName(); target_record_sys_id = current.getUniqueValue(); new si_PDF_Generator().generatePDF(html_content, file_name, target_record_table, target_record_sys_id); } })(current, previous);

 

script include

var si_PDF_Generator = Class.create(); si_PDF_Generator.prototype = Object.extendsObject(AbstractAjaxProcessor, { generatePDF: function(html_content, file_name, target_record_table, target_record_sys_id) { if (html_content == undefined) html_content = this.getParameter('sysparm_html_content'); if (file_name == undefined) file_name = this.getParameter('sysparm_file_name'); if (target_record_table == undefined) target_record_table = this.getParameter('sysparm_target_record_table'); if (target_record_sys_id == undefined) target_record_sys_id = this.getParameter('sysparm_target_record_sys_id'); var document = null; var pdfDoc = new GeneralPDF.Document(null, null, null, null, null, null); document = new GeneralPDF(pdfDoc, null, null); document.startHTMLParser(); document.addHTML(html_content); document.stopHTMLParser(); var att = new GeneralPDF.Attachment(); att.setTableName(target_record_table); att.setTableId(target_record_sys_id); att.setName(file_name); att.setType('application/pdf'); att.setBody(document.get()); GeneralPDF.attach(att); }, type: 'si_PDF_Generator' });



(ServiceNow )

Comments

Popular posts from this blog

ServiceNow timeout settings for virtual agent chat

  After connecting to live agent User is connected to live agent and not responding to live agent chat or distracted. The below properties controls the timeout settings, the OOB settings for reminder is [180 sec] and for cancelling the chat is [360 sec]. The job is default configured to 2 min so I believe no tweaking is required here.  Property -   com.glide.cs.idle_chat_reminder_timeout                  com.glide.cs.idle_chat_cancel_timeout Scheduled job   - Idle Chat Timer Task   https://community.servicenow.com/community?id=community_article&sys_id=1453b03bdbaad0109e691ea668961929   (ServiceNow ) 

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){...