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

Get URL Parameter - server side script (portal or classic UI)

Classic UI : var sURL_editparam = gs . action . getGlideURI (). getMap (). get ( ' sysparm_aparameter ' ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); } Portal : var sURL_editparam = $sp . getParameter ( " sysparm_aparameter " ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); }

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 )){