logo

NJP

Autopopulating PDF with Form data and attaching it to the ticket

Import · Jun 14, 2018 · article

Recently, working for a client the requirement was really interesting. They have a 4-page form for casual hire. The first 3 pages are to be filled by a nominator and the last one by the nominee. So essentially I needed to make a 3-page pdf form into a Catalogue Item, and for the fourth part, when pdf goes to the nomine, they need to check the first 3-part form as filled up by the nominator.

I came up with the solution that, if the nominee is an existing employee then he/she can access the request item with the link of the request item, but the issue is with the nominee being external entity. In that case, I suggested that the notification to the nominee contains a pdf file that will capture everything that is filled by the nominator in the catalogue item.

Auto filling PDF with catalogue item data is a bit tricky one and there isn’t much documentation about it so I am sharing my idea and methods that I implemented for this.

There are two ways of doing it:

1st method is the “.write” function.

Syntax: attachment.write('tablename', recordsysid, fileName, content_type, fileDate);

“attachment” is the object variable of Attachment() type

“tablename” is string variable that contains the name of the table where you want to attach the file.

“recordsysid” is the sys id of the record where you want to attach the file

“filename” is the file name of the attachment

“content_type” is the type of content in the file (usually for pdf it is ‘text/text’)

“fileData” contains the data that you want to put in the file.

Example: Business Rule to run after insert

var tableName = "sc_req_item"; //Request Item Table

var recordsysid = current.sys_id; //sys id of the record

var fileName = current.number+" FormData.pdf";

var content_type = 'text/pdf';

var fileData=""; // Declare an empty string

fileData+=current.number+" "+current.short_description+"</ b>"; // current access the varibles in the record

fileData+=current.variables.u_requested_for.getDisplayValue()+"</ b>"; // current.variables access the variables or fields in the form or catalog Item

fileData+=current.variables.u_department.getDisplayValue();

var attachment = new Attachment();

attachment.write('tablename', recordsysid, fileName, content_type, fileDate);

While this method is good but it won’t support any formatting of the pdf layout. So you get a basic pdf with the data and you cannot put heading and other formatting details. Hence the 2nd method.

2nd Method is using the global.GeneralPdfUtils() of SNOW.

Syntax: pdf.prefillPdf(jsonString, destinationTableSysId, attachmentSysId, destinationTableName, pdfName);

“pdf” is an instance object of global.GeneralPdfUtils()

“jsonString” contains the data that you gonna insert into the pdf in JSON format (it needs to be in JSON for a reason)

“destinationTableSysId” sys id of the record where you are attaching the file

“attachmentSysId” sys id of the pdf template that you need to upload first, in which the data will be filled

“destinationTableName” table name of the record where you are attaching the file

“pdfName” naming the pdf file to be attached

Example: Business Rule to run after insert

var attachmentSysId="56d921c4db3a9f806223147a3a9619ea";

var pdf = new global.GeneralPdfUtils();

var jsonObject = {

"title_category":current.variables.u_title_category.getDisplayValue(),

"name_nominator":current.variables.u_requested_for.getDisplayValue(),

"school_nominator":current.variables.u_school_centre_section.getDisplayValue(),

"mail_nominator":current.variables.u_mailing_address.getDisplayValue(),

"email_nominator":current.variables.u_email_address.getDisplayValue()}

var jsonString = JSON.stringify(jsonObject);

var destinationTableName = 'sc_req_item';

var destinationTableSysId = current.sys_id;

var pdfName = current.number+" Application Details.pdf";

pdf.prefillPdf(jsonString, destinationTableSysId, attachmentSysId, destinationTableName, pdfName);

TWO IMPORTANT THINGS ABOUT THIS;

The reason why we need a JSON object is because we are actually mapping the data. So first thing, you need to create a PDF template, I created a word file (because I find it easier to type heading and all in word) and then converted it into a pdf. Opened the file in Adobe Acrobat and opted for the “Forms>>Add Edit Fields”. This let you create fields in the pdf. I used checkboxes and text input fields. Once you create a field, name the field, and use the same name as object propertyName.

For instance, form the above example, I named a field “title_category” in the pdf and so the JSON object has a propertyname “title_category” and then you assign the value of this specific property as “current.variables.u_title_category.getDisplayValue()” so this will fetch the data from the form field named ‘u_title_category’.

And that should work just fine.

I am attachingsample files (Output PDF, PDF Template and sample code)

Hope it helps someone image

View original source

https://www.servicenow.com/community/developer-articles/autopopulating-pdf-with-form-data-and-attaching-it-to-the-ticket/ta-p/2313317