The CreatorCon Call for Content is officially open! Get started here.

Sandeep Rajput
Tera Patron
Tera Patron

Recently I came across a question How to edit PDF attachment with record details where the questioner was facing issues while using the GeneralPDFUtils while generating the PDFs with pre-populated information fetched from a record. 

 

Though there are several ways using which the PDF can be generated in ServiceNow once such example is https://www.servicenow.com/community/developer-articles/generating-custom-pdfs-using-the-new-pdfgene... which uses PDFGeneration API.

 

Once such alternative is GeneralPdfUtils which is a script include available in ServiceNow which allows populating Fillable PDFs with values fetched from any record on the fly.

 

Here is an example how I used this script include to fetch the available fields from the fillable PDF, and populate the data into it.

 

Here is the sample script I used.

 

Here I attached my fillable PDF on an incident record and populated values in it by fetching them from the incident record.

 

 

var list = new GlideRecord("incident");
if (list.get('e5cedec14776611092c98021336d438d')){
    var attachment = new GlideRecord('sys_attachment');
    if(attachment.get('table_sys_id',list.getValue('sys_id'))){
        gs.info(attachment.getValue('file_name'));

        var pdfUtils = new global.GeneralPdfUtils();
        gs.info(pdfUtils.getPDFFields(attachment.sys_id));//Provides you the names of fields within PDF
        var jsonForPDF = '{"Check Box1":"","Check Box2":"","Address":"'+list.caller_id.company.street+ ' '+list.caller_id.company.city +', '+list.caller_id.company.state+', '+list.caller_id.company.zip+'","Text6":"This is fillable","Text5":"PDF","Name":"'+list.caller_id.first_name+' '+list.caller_id.last_name+'","Group6":"","Dropdown3":"","Dropdown2":"","Dropdown1":"","Check Box3":"false","Button7":"","Check Box4":"true"}';
        //gs.print(jsonForPDF)
        pdfUtils.prefillPdf(jsonForPDF,list.getValue('sys_id'),attachment.getValue('sys_id'),'incident','filled_pdf'+Math.random()+'.pdf');
        
        
    } 
       
}

 

 

 

 

In order to be sure about the field names present in the fillable PDF, use the following method.

 

 

 

pdfUtils.getPDFFields(attachment.sys_id); //Provides the list of fillable field names in a JSON format.

 

 

 

 

Here is how my fillable PDF looks when it is empty. 

Screenshot 2023-05-21 at 6.56.07 PM.png

Here is how it looks when the data is filled into it using the script shared earlier.

Screenshot 2023-05-21 at 10.33.14 PM.png

Attached is the fillable PDF for your reference. Test it by attaching it on any incident record and use the sys_id of that incident on line of two of this script.

 

 

 

list.get('e5cedec14776611092c98021336d438d'); //Change this with your incident sample record.

 

 

 

Hope this helps.

Version history
Last update:
‎05-21-2023 10:07 AM
Updated by:
Contributors