PDF Generation for Policy Exception

ShafrazMubarak
Giga Guru

I have a business requirement of generating the GRC Policy Exception record as PDF and attach the document in Policy Exception approval emails. as there is no direct way of doing this, based on a forum article, I created a HTML template in 'Document Templates" module. 

 

Then, I created a record in script includes as follows 

var PolicyExceptionPDFGenerator = Class.create();
PolicyExceptionPDFGenerator.prototype = {
    initialize: function() {},

    generatePDF: function(recordSysId) {
        var gr = new GlideRecord('sn_compliance_policy_exception'); 
        if (gr.get(recordSysId)) {
            var htmlTemplate = GlideSysAttachment.getContentStream(gr.sys_id, '0f48ac2cf0df121070bd59e0c86bba81'); 
            var pdfAPI = new sn_pdfgeneratorutils.PDFGenerationAPI();
            var pdf = pdfAPI.convertToPDF(htmlTemplate, 'sn_compliance_policy_exception', recordSysId, 'Policy_Exception_Report.pdf'); 
            return pdf;
        }
        return null;
    },

    attachPDFToRecord: function(recordSysId, pdf) {
        var gr = new GlideRecord('sn_compliance_policy_exception'); 
        if (gr.get(recordSysId)) {
            var attachment = new GlideSysAttachment();
            attachment.write(gr, 'Policy_Exception_Report.pdf', 'application/pdf', pdf);
        }
    },

    type: 'PolicyExceptionPDFGenerator'
};

 

Then i created a business rule on policy exception table that triggers when the State change to Awaiting Approval  

 

(function executeRule(current, previous /*null when async*/) {
    var pdfGenerator = new PolicyExceptionPDFGenerator();
    var pdf = pdfGenerator.generatePDF(current.sys_id);
    if (pdf) {
        pdfGenerator.attachPDFToRecord(current.sys_id, pdf);
    }
})(current, previous);

 

But when i am trying to generate the approvals, I noticed that this is not getting attache to the record. 

 

Any idea what is missing here?

__PRESENT

__PRESENT

__PRESENT

__PRESENT

__PRESENT

__PRESENT

__PRESENT

__PRESENT

__PRESENT

__PRESENT

__PRESENT

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ShafrazMubarak 

are you having a requirement to include the PDF in particular look and feel?

If yes then only use Document template

If not then you can create a form view consisting of fields you want and then refer below link to generate the pdf and attach to record,

Generate PDF and attach to the record 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@ShafrazMubarak 

are you having a requirement to include the PDF in particular look and feel?

If yes then only use Document template

If not then you can create a form view consisting of fields you want and then refer below link to generate the pdf and attach to record,

Generate PDF and attach to the record 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@ShafrazMubarak 

you can use this script to attach

(function executeRule(current, previous /*null when async*/) {
   var recordId = current.sys_id;
   var pdfName = 'Policy_Exception_Report.pdf';
   var documentTemplateId = '''; // give sysId of that document template
   new sn_doc.GenerateDocumentAPI().generateDocumentForTask(recordId, documentTemplateId, pdfName);
})(current, previous);


If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@ShafrazMubarak 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

ShafrazMubarak
Giga Guru

Yes. I have the requriement to generate the PDF with certain look and feel (Header Logos, Tables, Alignments and etc). Therefore, I need to use Document Template. I will try the script and respond back today