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

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue' href=hr_profile.do?sysparm_query=number=" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object