How to Generate and Embed a QR Code in a Document Template in ServiceNow

amrfahmy2
Tera Contributor

📎 How to Generate and Embed a QR Code in a Document Template in ServiceNow

This article explains how to dynamically generate a QR Code in ServiceNow and embed it into a Document Template using a Document Template Script. This is useful for adding QR codes to PDF or Word documents such as tickets, receipts, confirmations, or forms.

🔹 Step 1: Import and Commit the Update Set

  1. attached file
This update set includes the qrGeneration template script using global.HtmlRenderQRCode().

🔹 Step 2: Insert the QR Code Placeholder in Your Document

  1. Navigate to All → Document Template → Document Template.
  2. Open the required document template record.
  3. In the Template Body field, insert the following placeholder where the QR Code should appear:
${template_script:qrGeneration}

🔹 Step 3: Create the Document Template Script

Navigate to All → Document Template → Document Template Scripts and click New. Use the following script:

(function runTemplateScript(target /*GlideRecord for target task*/, docTemplate /*GlideRecord for doc template*/) { var url = gs.getProperty('glide.servlet.uri') + "?id=ticket&sys_id=" + target.sys_id;
var htmlRender = global.HtmlRenderQRCode(url);
return htmlRender;
})(target, docTemplate);
  • Name: qrGeneration
  • Applies to: Set the relevant table (e.g., Case, Task)
📝 Ensure the glide.servlet.uri system property exists. If not, hardcode the URL for testing.

🔹 Step 4: Test the Document Generation

  1. Navigate to All → Document Template → Document Template.
  2. Open your configured document template.
  3. Click the Preview button.
  4. Select a specific record (e.g., case or task).
  5. Click Generate.
The generated document should now display the QR Code in the placeholder's location.

📌 Summary

Step Action

1Import the update set and commit it
2Insert ${template_script:qrGeneration} in the document template
3Create the Document Template Script using HtmlRenderQRCode()
4Use the Preview button and test with a specific record
2 REPLIES 2

IreneAntoli
Tera Contributor

Hi,

I may be doing something wrong, but when I import the update set in my DPI I see 0 updates.

Regarding your solution, can it be used to render QR codes in notifications?

Thank you and kind regards,

ajmalmuhamm
Tera Contributor

Hi @amrfahmy2 ,

Thanks for sharing this! This is a helpful approach for dynamically generating QR codes in Document Templates.

One thing I'd add is to ensure the glide.servlet.uri system property is configured correctly, as the generated QR code relies on it to build the record URL. Also, make sure the Document Template Script is applied to the correct table; otherwise, the placeholder won't render during document generation.

Overall, this is a simple and effective solution for embedding QR codes into generated PDF or Word documents without requiring additional custom integrations.