Unleash the Automation: The Hidden Power of applyEncodedQuery and applyTemplate
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
In the world of ServiceNow development, we often get caught up in complex GlideRecord loops and manual field assignments. While current.short_description = '...' works fine for a single field, it’s not exactly scalable or "clean" when you’re dealing with dynamic data.
If you want to write more maintainable, efficient, and readable code, it’s time to master two of the most underutilized methods in the GlideRecord API: applyEncodedQuery() and applyTemplate().
1. applyEncodedQuery(): The Mass Mapper
Most developers use encoded queries to filter data (e.g., addEncodedQuery()). However, applyEncodedQuery() does the opposite: it sets values on a record based on a query string.
Why use it?
Instead of writing 20 lines of code to set 20 fields, you can pass a single string. This is incredibly useful when pulling configurations from a system property or a "mapping" table.
var gr = new GlideRecord('incident'); gr.initialize(); // Instead of setting fields individually... var queryString = "priority=1^short_description=Critical System Outage^category=software"; gr.applyEncodedQuery(queryString); gr.insert();
Pro-Tip:
This method is perfect for dynamic integrations. If an external system sends a payload of "key-value" pairs that match ServiceNow field names, you can construct a query string and apply it instantly.
2. applyTemplate(): Consistency is King
We’ve all seen (and hopefully used) Templates in the ServiceNow UI to manually fill out forms. But did you know you can trigger that same logic via script?
applyTemplate("Template Name") allows you to leverage the work your process owners have already done in the UI.
Why use it?
Maintenance: If the "New Hire" template needs a new default description, a non-coder can update the Template record without you touching a single line of script.
Logic: It respects the template's ability to bypass certain validations or include specific task structures.
var gr = new GlideRecord('change_request'); gr.initialize(); // Apply the 'Standard Server Reboot' template gr.applyTemplate('Standard Server Reboot'); // You can still override specific fields after applying gr.requested_by = gs.getUserID(); gr.insert();
Comparing the Two
| Feature | applyEncodedQuery | applyTemplate |
| Source | String/Variable | Template Record (sys_template) |
| Best For | Dynamic API data/System Props | Standardized Business Processes |
| Complexity | Developer-driven | Admin/Process Owner-friendly |
| Flexibility | Extremely high | Tied to a specific record |
When to Use Which?
Use applyEncodedQuery when the data is coming from a dynamic source (like a CSV import or a JSON response) where you need to map many fields on the fly without hardcoding every gr.field = value.
Use applyTemplate when you want to empower your business users. Let them manage the "default values" in a Template record, while your script simply acts as the engine that triggers it.
Final Thought
Stop hardcoding. By using these two methods, you separate your logic (the script) from your data (the values), leading to a cleaner instance and a much happier development lifecycle.
If this helped, please mark it helpful.
Thanks!
Krishnamohan
