top of page

Mastering GlideRecord Queries: How to Use 'STARTSWITH' in ServiceNow

  • davidyang88
  • Mar 20
  • 2 min read

Updated: Mar 29

GlideRecord is a powerful feature in ServiceNow that allows users to efficiently query and manage data records. Often, administrators or developers need to find specific records where field values begin with a particular string, such as retrieving database tables with names starting with "cmdb." However, confusion may arise on how to properly structure these queries. In this article, we clarify exactly how to use the 'STARTSWITH' condition in GlideRecord queries.


Understanding the GlideRecord 'STARTSWITH' Query


When querying records using GlideRecord, it's common to encounter scenarios where you need to fetch records based on field values that begin with specific characters or words. A typical use case is when administrators want to identify database objects whose names start with certain prefixes, such as "cmdb," to manage configuration items efficiently.


Step-by-Step Guide to Using 'STARTSWITH' in GlideRecord


Below are verified methods to implement 'STARTSWITH' in your GlideRecord queries.


Method 1: Using the addEncodedQuery Method


This method leverages the simplicity of encoded queries, ideal for concise queries:

var gr = new GlideRecord('sys_db_object');
gr.addEncodedQuery("nameSTARTSWITHcmdb");
gr.query();
while (gr.next()) {
   gs.addInfoMessage(gr.sys_id);
}

This query retrieves all records from the sys_db_object table whose names begin with "cmdb" and displays their sys_id.


Method 2: Using the addQuery Method


Alternatively, you can explicitly structure your GlideRecord query using the addQuery function. This method is recommended by ServiceNow for clarity and maintainability:

var gr = new GlideRecord('sys_db_object');
gr.addQuery('name', 'STARTSWITH', 'cmdb');
gr.query();

while (gr.next()) {
   gs.addInfoMessage(gr.sys_id);
}

This explicit method provides greater readability, especially beneficial for newer ServiceNow developers or administrators.


Common Issues and Practical Solutions


One frequent confusion is the improper formatting of the encoded query, leading to no records being returned. Ensure that you have the correct syntax without spaces when using the addEncodedQuery method, as spaces can invalidate your query.


Example of Incorrect Usage:


// Incorrect Query (note spaces)
gr.addEncodedQuery("name STARTSWITH cmdb");

This syntax will fail. Always eliminate spaces to properly execute the query:

// Correct Query (no spaces)
gr.addEncodedQuery("nameSTARTSWITHcmdb");

Alternative Approaches


Though GlideRecord is highly versatile, for simpler data queries, you might also use GlideAggregate, especially if you need grouped or summarized data from tables beginning with certain strings.


Conclusion


Effectively leveraging GlideRecord queries like 'STARTSWITH' can significantly enhance your productivity within ServiceNow. Clear syntax and understanding of these queries streamline data management tasks, ultimately benefiting your operational efficiency.


Next Steps


  • Practice different GlideRecord queries regularly to reinforce your understanding.

  • Explore advanced querying techniques like GlideAggregate for complex reporting needs.

  • Stay updated with ServiceNow documentation to adapt to best practices and new features.

CONTACT

New Zealand HQ

Integrated Knowledge Consulting Office

Level 3, 93 Grafton Road

Auckland

South Korea

Integrated Knowledge Consulting Office

BMY역삼타워 6층

서울특별시 강남구 역삼동 678-10번지

 

info@ikconsulting.com

Thanks for submitting!

  • LinkedIn Social Icon

© Copyright 2025 Integrated Knowledge Consulting. All rights reserved.

bottom of page