<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>post Scripting, Performance, and Queries, OH MY in Community Resources</title>
    <link>https://www.servicenow.com/community/community-resources/scripting-performance-and-queries-oh-my/ta-p/3379309</link>
    <description>&lt;P&gt;One of the things I've found over the years pretty frequently is the question from junior devs asking about how they can make their 'code' more performant.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There's stuff everywhere and I haven't seen one place that has these little tips and tricks so here we go!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H2&gt;1. Use get().&lt;/H2&gt;&lt;P&gt;Seriously use get() where you can. If you have the sys_id for an object and are just trying to return one record this is super easy peasy. You don't even HAVE TO HAVE A SYS_ID. You can use a unique value!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are some examples for how to use it!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can return a specific record:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var gr = newGlideRecord('incident');
gr.get('ENTER-SYS-ID-HERE');&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can try to return a specific record and if it doesn't find it, do something:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var gr = newGlideRecord('incident');
if(gr.get('ENTER-SYS-ID-HERE')){
//do something because it found the record
} else {
//do something else because you didn't find the record
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can use a specific field that holds a unique value:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var incNumber = 'INC0012345';
var gr = newGlideRecord('incident');
gr.get('number', incNumber);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H2&gt;2. Use setLimit()&lt;/H2&gt;&lt;P&gt;If you are scripting out a query and need to return 1, single record: use setLimit( 1 ). If you are trying to return 10 records, use setLimit(10). This means that you are not going to query the entire table for that result and that means that it'll return faster!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are some examples for how to use it!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I want to return 1 record:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var incNumber = 'INC0012345';
var gr = newGlideRecord('incident');
gr.addQuery('number', incNumber);
gr.setLimit( 1 ); //spaces added for web formatting - remove the spaces between the 1 and the ( )
gr.query();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I want to return any 10 records for a query:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var ci = 'OfficePC2';
var gr = newGlideRecord('incident');
gr.addQuery('cmdb_ci', ci);
gr.setLimit(10);
gr.query();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H2&gt;3. Short-circuit&lt;/H2&gt;&lt;P&gt;Using the boolean || will allow you to 'short-circuit' your logic. This means that if you evaluate whether A or B = 1, if it evaluates A = 1, it'll skip B allowing for faster processing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, if you are doing this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;if (name.startsWith('a')){
   return true;
} else if (name.startsWith('b')) {
   return false;
} else if (name.startsWith('c')) {
   return true;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can instead do this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;if(name.startsWith('a') || name.startsWith('c') ){
  return true;
} else {
  return false;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H2&gt;Now, if you're talking about queries, here's what ServiceNow has to say:&lt;/H2&gt;&lt;P class=""&gt;Common reasons for slow queries&lt;/P&gt;&lt;DIV class=""&gt;&lt;UL class=""&gt;&lt;LI&gt;A query has too many OR conditions (for more information, see&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A class="" title="Use a &amp;quot;contains&amp;quot; query only in special cases, such as when users or groups need to see data from a domain that they don't have access to, but you don't want to move those users to a domain. Creating domain &amp;quot;contains&amp;quot; and user or group access for a domain should be an exception, only when absolutely needed." href="https://www.servicenow.com/docs/bundle/zurich-platform-security/page/administer/company-and-domain-separation/concept/bp-contains-domain-visibility.html" target="_blank" rel="noopener"&gt;Contains queries and domain access&lt;/A&gt;). In the domain hierarchy, place the user or a domain at a hierarchy level where contains or visibility is not needed.&lt;/LI&gt;&lt;LI&gt;The query method is not the domain path query method (for more information, see&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A class="" title="You can create effective queries with domain paths." href="https://www.servicenow.com/docs/bundle/zurich-platform-security/page/administer/company-and-domain-separation/concept/bp-domain-query-method.html" target="_blank" rel="noopener"&gt;Domain paths query method&lt;/A&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt; If you are not using the domain path query method, contact&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;Customer Service and Support&lt;/SPAN&gt;.&lt;/LI&gt;&lt;LI&gt;A query needs a database to be indexed so you can see what is in the database quickly. If you can identify the slow query, run the "explain plan" to see if there are options for indexing available. The "explain plan" is a function of SQL that shows the query and what is going on with it.&lt;/LI&gt;&lt;/UL&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="skeletor.jpg" style="width: 735px;"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/470543iB7B395BFEC327E29/image-size/large?v=v2&amp;amp;px=999" role="button" title="skeletor.jpg" alt="skeletor.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 23 Dec 2025 20:02:26 GMT</pubDate>
    <dc:creator>Ryan_Gillespie</dc:creator>
    <dc:date>2025-12-23T20:02:26Z</dc:date>
    <item>
      <title>Scripting, Performance, and Queries, OH MY</title>
      <link>https://www.servicenow.com/community/community-resources/scripting-performance-and-queries-oh-my/ta-p/3379309</link>
      <description>&lt;P&gt;One of the things I've found over the years pretty frequently is the question from junior devs asking about how they can make their 'code' more performant.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There's stuff everywhere and I haven't seen one place that has these little tips and tricks so here we go!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H2&gt;1. Use get().&lt;/H2&gt;&lt;P&gt;Seriously use get() where you can. If you have the sys_id for an object and are just trying to return one record this is super easy peasy. You don't even HAVE TO HAVE A SYS_ID. You can use a unique value!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are some examples for how to use it!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can return a specific record:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var gr = newGlideRecord('incident');
gr.get('ENTER-SYS-ID-HERE');&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can try to return a specific record and if it doesn't find it, do something:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var gr = newGlideRecord('incident');
if(gr.get('ENTER-SYS-ID-HERE')){
//do something because it found the record
} else {
//do something else because you didn't find the record
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can use a specific field that holds a unique value:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var incNumber = 'INC0012345';
var gr = newGlideRecord('incident');
gr.get('number', incNumber);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H2&gt;2. Use setLimit()&lt;/H2&gt;&lt;P&gt;If you are scripting out a query and need to return 1, single record: use setLimit( 1 ). If you are trying to return 10 records, use setLimit(10). This means that you are not going to query the entire table for that result and that means that it'll return faster!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are some examples for how to use it!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I want to return 1 record:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var incNumber = 'INC0012345';
var gr = newGlideRecord('incident');
gr.addQuery('number', incNumber);
gr.setLimit( 1 ); //spaces added for web formatting - remove the spaces between the 1 and the ( )
gr.query();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I want to return any 10 records for a query:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var ci = 'OfficePC2';
var gr = newGlideRecord('incident');
gr.addQuery('cmdb_ci', ci);
gr.setLimit(10);
gr.query();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H2&gt;3. Short-circuit&lt;/H2&gt;&lt;P&gt;Using the boolean || will allow you to 'short-circuit' your logic. This means that if you evaluate whether A or B = 1, if it evaluates A = 1, it'll skip B allowing for faster processing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, if you are doing this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;if (name.startsWith('a')){
   return true;
} else if (name.startsWith('b')) {
   return false;
} else if (name.startsWith('c')) {
   return true;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can instead do this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;if(name.startsWith('a') || name.startsWith('c') ){
  return true;
} else {
  return false;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H2&gt;Now, if you're talking about queries, here's what ServiceNow has to say:&lt;/H2&gt;&lt;P class=""&gt;Common reasons for slow queries&lt;/P&gt;&lt;DIV class=""&gt;&lt;UL class=""&gt;&lt;LI&gt;A query has too many OR conditions (for more information, see&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A class="" title="Use a &amp;quot;contains&amp;quot; query only in special cases, such as when users or groups need to see data from a domain that they don't have access to, but you don't want to move those users to a domain. Creating domain &amp;quot;contains&amp;quot; and user or group access for a domain should be an exception, only when absolutely needed." href="https://www.servicenow.com/docs/bundle/zurich-platform-security/page/administer/company-and-domain-separation/concept/bp-contains-domain-visibility.html" target="_blank" rel="noopener"&gt;Contains queries and domain access&lt;/A&gt;). In the domain hierarchy, place the user or a domain at a hierarchy level where contains or visibility is not needed.&lt;/LI&gt;&lt;LI&gt;The query method is not the domain path query method (for more information, see&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A class="" title="You can create effective queries with domain paths." href="https://www.servicenow.com/docs/bundle/zurich-platform-security/page/administer/company-and-domain-separation/concept/bp-domain-query-method.html" target="_blank" rel="noopener"&gt;Domain paths query method&lt;/A&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt; If you are not using the domain path query method, contact&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;Customer Service and Support&lt;/SPAN&gt;.&lt;/LI&gt;&lt;LI&gt;A query needs a database to be indexed so you can see what is in the database quickly. If you can identify the slow query, run the "explain plan" to see if there are options for indexing available. The "explain plan" is a function of SQL that shows the query and what is going on with it.&lt;/LI&gt;&lt;/UL&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="skeletor.jpg" style="width: 735px;"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/470543iB7B395BFEC327E29/image-size/large?v=v2&amp;amp;px=999" role="button" title="skeletor.jpg" alt="skeletor.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Dec 2025 20:02:26 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-resources/scripting-performance-and-queries-oh-my/ta-p/3379309</guid>
      <dc:creator>Ryan_Gillespie</dc:creator>
      <dc:date>2025-12-23T20:02:26Z</dc:date>
    </item>
  </channel>
</rss>

