Show Character Counter for Form Fields in ServiceNow Without Risky DOM Manipulation
📝 Summary:
When entering names or descriptions in fields like Client Scripts, Business Rules, or Scheduled Jobs, users often hit the maximum character limit unexpectedly. This results in rewriting and poor user experience.
This article provides a simple and safe solution that shows remaining characters for specific fields, improving form usability—without using unsafe DOM manipulation techniques.
💡 Use Case:
You're creating or editing records in tables like:
-
sys_script
(Business Rules) -
sys_client_script
(Client Scripts) -
sys_trigger
(Scheduled Jobs)
You try typing in the Name
or Short description
fields, and suddenly you hit a max character limit without any warning. This script shows the remaining characters live, just below the field, to avoid surprises.
✅ Key Features:
-
Works on standard forms (not just Service Portal)
-
No risky HTML DOM manipulation
-
Displays live character counter
-
Optionally shows warning message when limit is exceeded
-
Easy to extend to multiple fields and tables
🛠️ Script (Client Script – Type: onLoad
, UI Type: All
, Isolate Script: Unchecked
):
🔐 Why Isolate Script = Unchecked
?
To access DOM-like properties such as g_form.getControl(fieldName).getAttribute('maxlength')
, we must disable script isolation.
This is safe here because:
-
We’re only reading attributes.
-
We’re not modifying any layout or DOM nodes dangerously.
-
All changes are limited to UX enhancement, not structure.
🚀 How to Apply:
-
Navigate to System Definition > Client Scripts
-
Create a new onLoad Client Script
-
Target any table (like
sys_script
orsys_client_script
) -
Paste the code above
-
Uncheck "Isolate Script"
-
Test it on your form – you’ll now see a live character counter below the field(s)!
📦 Reusable & Extendable:
You can use the same logic for:
-
Any custom or out-of-box field with a character limit
-
Other tables by reusing the same script
-
Portal widgets (with slight changes)
⚠️ Note:
Avoid using document.getElementById()
or modifying HTML content directly. This solution stays within ServiceNow's supported boundaries while achieving a better user experience.
🙌 Result:
Your users will now see how many characters they have left as they type—making forms smarter, cleaner, and frustration-free!
Comments
Post a Comment