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
0 REPLIES 0