How to generate a Editable PDF with only populated Record Producer variables?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
I have a requirement where, upon submitting a Record Producer, a PDF is automatically generated based on the submitted variable values. The generated PDF is then attached to the created record.
Currently, the PDF displays all variables, including those that were left blank by the user.
Requirement:
I would like the PDF to display only the variables that have values. Any variables that were not populated during submission should be excluded from the generated PDF(Editable PDF).
For example:
- First Name: John ✔️ (should be displayed)
- Last Name: Smith ✔️ (should be displayed)
- Phone Number: (blank) ❌ (should not be displayed)
- Address: (blank) ❌ (should not be displayed)
In short, the PDF should be generated dynamically and include only the fields that contain values.
Has anyone implemented a similar requirement? Is there an out-of-the-box approach or a recommendation to achieve this?
Any suggestions would be greatly appreciated.
Thankyou!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hey @mania,
This is a scripting problem, not a template problem. With the PDF Generator plugin (sn_pdfgeneratorutils) against a fillable PDF, the template can carry every field, you just control what actually lands in the fieldMap object at generation time, so a blank variable never gets added and never renders. Here's the full flow:
- Design the PDF in Adobe Acrobat (or similar) with a named form field for every possible variable, then upload it as an editable PDF template in the PDF Generator application
- On the record producer, either use its own onSubmit script or an after-insert Business Rule on the target table, so you're working against a fully created current record with variables populated
- Query item_option_new filtered on cat_item = the producer's sys_id to get the list of variable names defined on it, so you're not hardcoding each one
- Loop through that list and only add a key to fieldMap when the value isn't blank, for example:
var fieldMap = {};
var gr = new GlideRecord('item_option_new');
gr.addQuery('cat_item', producerSysId);
gr.query();
while (gr.next()) {
var varName = gr.getValue('name');
var val = current.variables[varName].toString();
if (val != '') {
fieldMap[varName] = val;
}
}- Instantiate sn_pdfgeneratorutils.PDFGenerationAPI and call fillDocumentFieldsAndFlatten(fieldMap, templateSysId, tableName, recordSysId, pdfName, {"FlattenType": "donot_flatten"}) instead of the plain fillDocumentFields, so the output stays an editable AcroForm rather than getting auto-flattened
- Check result.status equals "success" and grab result.attachment_id, the API attaches the PDF to the record for you, no need to build the sys_attachment record yourself
One gotcha worth flagging up front: the keys in fieldMap have to match the PDF's internal form field names exactly, not the variable's question label, so line up your catalog variable names with your Acrobat field names before you build this, otherwise you're maintaining a separate translation map for nothing.
References
- Fill fields in a PDF using API "PDFGenerationAPI"
- Fill PDF using GeneralPdfUtils
- How to access catalog variables from a Business Rule script
Thank you,
Vikram Karety
Octigo Solutions INC
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
share the logic/script how you are generating the PDF?
is it via PDF API etc?
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader