<h2>GlideQuery - groupBy(String fields)</h2><br/><div style="overflow-x:auto"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta content="text/html; charset=UTF-8" /><meta name="copyright" content="(C) Copyright 2025" /><meta name="DC.rights.owner" content="(C) Copyright 2025" /><meta name="generator" content="DITA-OT" /><meta name="DC.type" content="concept" /><meta name="DC.title" content="GlideQuery - Scoped, Global" /><meta name="abstract" content="The GlideQuery API is an alternative to the GlideRecord API to perform CRUD operations on record data from server-side scripts." /><meta name="description" content="The GlideQuery API is an alternative to the GlideRecord API to perform CRUD operations on record data from server-side scripts." /><meta name="DC.relation" scheme="URI" content="../../../../../build/applications/concept/api-server.html" /><meta name="DC.relation" scheme="URI" content="../../../../../build/applications/concept/api-implementation-reference.html" /><meta name="DC.relation" scheme="URI" content="../../../../../build/applications/concept/api-reference.html" /><meta name="DC.creator" content="ServiceNow" /><meta name="DC.date.created" content="2023-08-03" /><meta name="DC.date.modified" content="2023-08-03" /><meta name="DC.format" content="XHTML" /><meta name="DC.identifier" content="GlideQueryGlobalAPI" /><link rel="stylesheet" type="text/css" href="../../../../../CSS/commonltr.css" /><title>GlideQuery - Scoped, Global</title></head><body class="api-class"> <div class="nested0" id="GlideQueryGlobalAPI"> <h1 class="title topictitle1" id="ariaid-title1">GlideQuery - Scoped, Global</h1> <div class="body conbody"><p class="shortdesc">The <span class="keyword apiname">GlideQuery</span> API is an alternative to the <span class="keyword apiname">GlideRecord</span> API to perform CRUD operations on record data from server-side scripts.</p> <p class="p">The <span class="keyword apiname">GlideQuery</span> API lets you:</p> <div class="p"><ul class="ul" id="GlideQueryGlobalAPI__ul_mkt_thc_nmb"><li class="li">Use standard JavaScript objects and types for queries and results.</li><li class="li">Quickly diagnose query errors with additional checks and clear error messages.</li><li class="li">Simplify your code by avoiding boiler-plate query patterns.</li><li class="li">Avoid common performance issues without needing deeper knowledge of GlideRecord.</li></ul> </div> <div class="p">Use the <span class="keyword apiname">GlideQuery</span> API in scoped or global server-side scripts. When used within a scoped app, it must be prefixed with the global scope. <pre class="pre codeblock"><code>new global.GlideQuery('sys_user') // ...</code></pre>This API requires the GlideQuery [com.sn_glidequery] plugin.</div> <div class="section" id="GlideQueryGlobalAPI__section_wqk_wwv_wmb"><h2 class="title sectiontitle">Implementation</h2> <p class="p">This API works together with the <a class="xref" href="../../Stream/concept/StreamGlobalAPI.html" title="The Stream API interacts with a stream of items such as records. For example, you can use the forEach() method to update the state of each record in a stream returned by the GlideQuery API."><span class="keyword apiname">Stream</span></a> and <a class="xref" href="../../Optional/concept/OptionalGlobalAPI.html" title="The Optional API interacts with a single record returned by the GlideQuery, Stream, or GlideRecord APIs, even when it does not exist. Write scripts that are less likely to result in an error by handling null or undefined query results."><span class="keyword apiname">Optional</span></a> APIs in a <span class="ph">builder pattern where the method calls chain together, each method building on the returned result of the previous method. Use methods to define the attributes of the query. The methods do not execute until you call a terminal method, a method that returns a query result, allowing you to define the requirements of the query before executing it.</span></p> <p class="p"><span class="ph">If the query returns a single record, the system wraps the result in an Optional object. If the query returns a stream of records, the system wraps the result in a Stream object. These objects let you manage the result using a set of methods in each API.</span></p> <p class="p"><span class="ph">For example, here's a script that performs a query on the Task table and groups the records by priority and returns groups with total reassignments greater than four.</span></p> <div class="p"><pre class="pre codeblock"><code>var query = new global.GlideQuery('task') .where('active', true) //Returns new GlideQuery object with a "where" clause. .groupBy('priority') //Returns new GlideQuery object with a "group by" clause. .aggregate('sum', 'reassignment_count') //Returns new GlideQuery object with a "sum(reassignment_count)" clause. .having('sum', 'reassignment_count', '>', 4) //Returns new GlideQuery object with a "having reassignment_count > 4" clause. .select() //Returns a stream of records wrapped in a Stream object. .toArray(10); //Terminal method in the Stream class that executes the query and returns the result. </code></pre></div> </div> <div class="section" id="GlideQueryGlobalAPI__section_kpr_hgc_nmb"><h2 class="title sectiontitle">Error handling</h2> <p class="p">The <span class="keyword apiname">GlideQuery</span> API throws an error when your query has a problem, and includes a clear explanation to help guide you. <span class="keyword apiname">GlideQuery</span> checks for:</p> <div class="p"><ul class="ul" id="GlideQueryGlobalAPI__ul_kpn_wwb_nmb"><li class="li">Invalid fields</li><li class="li">Invalid value types for a field</li><li class="li">Invalid values for choice fields</li><li class="li">Invalid query operators</li></ul> </div> <p class="p">For example, this code sample would throw an error because the queried field does not exist in the table.</p> <div class="p"><pre class="pre codeblock"><code>new global.GlideQuery('task') .where('id', '4717dfe5a9fe198100450448b2404c16') // should be 'sys_id' .select('description', 'severity') .toArray(100); // Error: Unable to find field 'id' in table 'task'. Known fields: active, activity_due, ...</code></pre></div> <p class="p">This code sample would throw an error because the data type of one of the arguments is incorrect.</p> <div class="p"><pre class="pre codeblock"><code>new global.GlideQuery('task') .where('priority', 'one') // priority is an integer (should be 1) .select('description', 'severity') .toArray(100); // Error: Unable to match value ['one'] with field 'priority' in table 'task'. Expecting type 'integer'</code></pre></div> </div> <div class="section" id="GlideQueryGlobalAPI__section_mwz_y2d_smb"><h2 class="title sectiontitle">Reuse</h2> <p class="p">Because <span class="keyword apiname">GlideQuery</span> objects are immutable, you can reuse them later in other parts of your code. For example, this script creates a query and then uses the GlideQuery object later to generate a report.</p> <div class="p"><pre class="pre codeblock"><code>var highPriorityTasks = new global.GlideQuery('task') .where('active', true) .where('priority', 1); generateReport(highPriorityTasks); notifyOwners(highPriorityTasks); var avgReassignmentCount = highPriorityTasks .avg('reassignment_count') .orElse(0) </code></pre></div> </div> <div class="section" id="GlideQueryGlobalAPI__section_qtg_rxb_nmb"><h2 class="title sectiontitle">Limitations</h2> <p class="p">The <span class="keyword apiname">GlideQuery</span> API does not support:</p> <div class="p"><ul class="ul" id="GlideQueryGlobalAPI__ul_ctl_mzb_nmb"><li class="li">Reading or writing to tables that do not allow access from other scopes.</li><li class="li">Reading encoded queries.</li><li class="li">GlideDate or GlideDateTime objects, which are read as JavaScript strings.</li><li class="li">FX currency fields.</li><li class="li">Updating journal field types.</li><li class="li">Queries with ambiguous conditional logic. For example, the following query is unclear because the system does not know whether to execute <code class="ph codeph">(active = true AND name != null) OR last_name = Luddy</code> or <code class="ph codeph">active = true AND (name != null OR last_name = Luddy)</code>. <pre class="pre codeblock"><code>var user = new global.GlideQuery('sys_user') .where('active', true) .whereNotNull('name') .orWhere('last_name', 'Luddy') .selectOne() .get()</code></pre> <p class="p">See the <a class="xref" href="GlideQueryGlobalAPI.html#GQ-where_S_S" title="Adds a Where clause to the query that returns values based on a given condition."><span class="keyword apiname">where()</span></a> method to understand how to nest a child query instead.</p> </li></ul> </div> <div class="p"><div class="note"><span class="notetitle">Note:</span> Because the <span class="keyword apiname">GlideQuery</span> API converts GlideRecord objects into standard JavaScript objects, it may take longer to execute queries. To reduce performance issues, avoid creating loops that iterate over large numbers of records.</div> </div> </div> <div class="section" id="GlideQueryGlobalAPI__section_sdj_w3s_s5b"><h2 class="title sectiontitle">Intermediate and terminal functions in GlideQuery</h2> <p class="p"><span class="keyword apiname">GlideQuery</span> uses two categories of methods: intermediate and terminal. Intermediate functions are those functions which return a <a class="xref" href="../../Stream/concept/StreamGlobalAPI.html" title="The Stream API interacts with a stream of items such as records. For example, you can use the forEach() method to update the state of each record in a stream returned by the GlideQuery API."><span class="keyword apiname">Stream</span></a> (an API used for interacting with a stream of items like records), allowing a fluent style of syntax where calls are chained together. Terminal functions are those functions that do not return a Stream and therefore stop the chain of <span class="keyword apiname">Stream</span> method calls.</p> <p class="p">In <span class="keyword apiname">GlideQuery</span>, methods like where, orderBy, and disableWorkflow are intermediate functions which return a new <span class="keyword apiname">GlideQuery</span> object. Likewise, <span class="keyword apiname">GlideQuery</span>'s most popular terminal functions, select and selectOne, are terminal functions; they are called when <span class="keyword apiname">GlideQuery</span> is done being configured and is ready to start processing record(s). The differences between these two method types is important to keep in mind when configuring calls to the <span class="keyword apiname">GlideQuery</span> API according to your specific use cases. For more information about intermediate and terminal functions, see the article <a class="xref" href="https://developer.servicenow.com/blog.do?p=/post/glidequery-p6/" target="_blank" rel="noopener noreferrer"><span class="keyword apiname">GlideQuery</span> - Stream Processing Part 1</a>.</p> <p class="p">The following methods are classified as intermediate:</p> <ul class="ul" id="GlideQueryGlobalAPI__ul_nfz_knc_w5b"><li class="li">aggregate(String aggregateType, String field)</li><li class="li">disableAutoSysFields()</li><li class="li">disableWorkflow()</li><li class="li">forceUpdate()</li><li class="li">groupBy(String fields)</li><li class="li">having(String aggregateType, String field, String operator, Number value)</li><li class="li">limit(Number limit)</li><li class="li">orderBy(String fields)</li><li class="li">orderByDesc(String fieldOrAggregate, String field)</li><li class="li">orWhere(String fieldOrQuery, String operator, Any value)</li><li class="li">orWhereNotNull(String field)</li><li class="li">orWhereNull(String field)</li><li class="li">where(String fieldOrQuery, String operator, Any value)</li><li class="li">whereNotNull(String field)</li><li class="li">whereNull(String field)</li><li class="li">withAcls()</li></ul> <p class="p">The following methods are classified as terminal:</p> <ul class="ul" id="GlideQueryGlobalAPI__ul_d5p_t4c_w5b"><li class="li">avg(String field)</li><li class="li">count()</li><li class="li">deleteMultiple()</li><li class="li">get(String key, Array selectedFields)</li><li class="li">getBy(Object keyValues, Array selectedFields)</li><li class="li">insert(Object keyValues, Object selectedFields)</li><li class="li">insertOrUpdate(Object changes, Object selectedFields)</li><li class="li">max(String field)</li><li class="li">min(String field)</li><li class="li">select(String fields)</li><li class="li">selectOne(String fields)</li><li class="li">sum(String field)</li><li class="li">toGlideRecord()</li><li class="li">update(Object changes, Object selectedFields)</li><li class="li">updateMultiple(Object changes)</li></ul> <div class="p"><div class="note"><span class="notetitle">Note:</span> parse(String table, String encoded_query) is neither intermediate or terminal, but static.</div> </div> </div> </div> <div class="related-links"> <div class="familylinks"> <div class="parentlink"><strong>Parent Topic:</strong> <a class="link" href="../../../../../build/applications/concept/api-server.html" title="Use server APIs in scripts to change the functionality of applications, or when you create new applications.">Server API reference</a></div> </div> </div><div class="topic reference nested1 api-constructor" id="GQ-GlideQuery_S_O"> <h2 class="title topictitle2" id="ariaid-title2">GlideQuery - GlideQuery(String table)</h2> <div class="body refbody"><p class="shortdesc">Instantiates a GlideQuery object used to build and execute record queries.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-GlideQuery_S_O__table_z3r_skc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 1. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e511">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e514">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e517">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e511 ">table</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e514 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e517 ">Table to query.</td></tr></tbody></table> </div> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-GlideQuery_S_O__table_l23_ps3_yr" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 2. </span>Returns</span></caption><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e555">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e558">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e555 ">None</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e558 "> </td></tr></tbody></table> </div> <div class="example"> <p class="p">This example instantiates a query of the User table.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('sys_user');</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-aggregate_S_S"> <h2 class="title topictitle2" id="ariaid-title3">GlideQuery - aggregate(String aggregateType, String field)</h2> <div class="body refbody"><p class="shortdesc">Aggregates a field using a specified aggregation function.</p> <div class="section"> <p class="p">Use this method to build queries that aggregate against multiple fields or use multiple aggregate functions, or if you must use the <span class="keyword apiname">groupBy()</span> method. If you only want to aggregate against one field with one function, and you don't need to use <span class="keyword apiname">groupBy()</span>, then use one of these methods instead:</p> <div class="p"><ul class="ul" id="GQ-aggregate_S_S__ul_fxl_wyl_4mb"><li class="li"><span class="keyword apiname">avg()</span></li><li class="li"><span class="keyword apiname">min()</span></li><li class="li"><span class="keyword apiname">max()</span></li><li class="li"><span class="keyword apiname">count()</span></li></ul> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-aggregate_S_S__table_thy_rmc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 3. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e662">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e665">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e668">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e662 ">aggregateType</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e665 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e668 ">The type of aggregation function to perform. Options include:<div class="p"><ul class="ul" id="GQ-aggregate_S_S__agg-type-options"><li class="li"><code class="ph codeph">min</code>: Returns the smallest value of all matching records.</li><li class="li"><code class="ph codeph">max</code>: Returns the largest value of all matching records.</li><li class="li"><code class="ph codeph">sum</code>: Returns the sum of all matching records.</li><li class="li"><code class="ph codeph">avg</code>: Returns the average of all matching records.</li><li class="li"><code class="ph codeph">count</code>: Returns the number of number of matching records.</li></ul> </div> </td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e662 ">field</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e665 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e668 ">Field on which to perform the operation.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-aggregate_S_S__table_uhy_rmc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 4. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e745">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e748">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e745 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e748 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example performs a query on the Task table that groups the records by priority, adds the numbers in the reassignment count field for each group, and returns groups with total reassignments greater than four.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('task') .where('active', true) //Returns the GlideQuery object to add more attributes to the query. .groupBy('priority') //Returns the GlideQuery object to add more attributes to the query. .aggregate('sum', 'reassignment_count') //Returns the GlideQuery object to add more attributes to the query. .having('sum', 'reassignment_count', '>', 4) //Returns the GlideQuery object to add more attributes to the query. .select() //Returns a stream of records wrapped in a Stream object. .toArray(10); //Terminal method in the Stream class that executes the query and returns the result. gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "group":{ "priority":1 }, "sum":{ "reassignment_count":11 } }, { "group":{ "priority":3 }, "sum":{ "reassignment_count":6 } }, { "group":{ "priority":5 }, "sum":{ "reassignment_count":5 } } ]</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-avg_S"> <h2 class="title topictitle2" id="ariaid-title4">GlideQuery - avg(String field)</h2> <div class="body refbody"><p class="shortdesc">Returns the aggregate average of a given numeric field.</p> <div class="section"> <div class="p">You can only use this method on fields of the following types:<ul class="ul" id="GQ-avg_S__ul_ikp_2wl_qmb"><li class="li">Integer</li><li class="li">Long</li><li class="li">Floating Point Number</li><li class="li">Double</li><li class="li">Currency</li></ul> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-avg_S__table_lws_hmc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 5. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e852">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e855">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e858">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e852 ">field</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e855 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e858 ">Field on which to perform the operation.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-avg_S__table_mws_hmc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 6. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e895">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e898">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e895 ">Optional</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e898 ">Object that contains the aggregate average of the given field.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to return the average number of faults in the cmdb_ci table.</p> <pre class="pre codeblock"><code>var faults = new global.GlideQuery('cmdb_ci') .avg('fault_count') .orElse(0); gs.info(JSON.stringify(faults));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>0.0037</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-count"> <h2 class="title topictitle2" id="ariaid-title5">GlideQuery - count()</h2> <div class="body refbody"><p class="shortdesc">Returns the number of records that match the query.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-count__table_qs4_4mc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 7. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e981">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e984">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e987">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e981 ">None</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e984 "> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e987 "> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-count__table_rs4_4mc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 8. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1022">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1025">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1022 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1025 ">Number of records that match the query.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example returns the number of active records in the User table.</p> <pre class="pre codeblock"><code>var userCount = new global.GlideQuery('sys_user') .where('active', true) .count();</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>612</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-deleteMultiple"> <h2 class="title topictitle2" id="ariaid-title6">GlideQuery - deleteMultiple()</h2> <div class="body refbody"><p class="shortdesc">Deletes all records in the table specified by the preceding Where clauses.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-deleteMultiple__table_gyp_nlc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 9. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1109">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1112">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1115">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1109 ">None</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1112 "> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1115 "> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-deleteMultiple__table_hyp_nlc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 10. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1150">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1153">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1150 ">None</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1153 "> </td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example deletes all active records in the User table where the last name is Jeter.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('sys_user') .where('active', true) .where('last_name', 'Jeter') .deleteMultiple();</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-disableAutoSysFields"> <h2 class="title topictitle2" id="ariaid-title7">GlideQuery - disableAutoSysFields()</h2> <div class="body refbody"><p class="shortdesc">Disables updating system fields, or fields with a name that starts with the <code class="ph codeph">sys</code> prefix, such as sys_created_on, sys_updated_on, and sys_mod_count. Only applies to the specified query.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-disableAutoSysFields__table_xnr_wlc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 11. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1232">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1235">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1238">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1232 ">None</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1235 "> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1238 "> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-disableAutoSysFields__table_ynr_wlc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 12. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1273">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1276">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1273 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1276 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example adds a record to the task table, but does not set system fields. Without calling this method, the below example would update sys_updated_on, sys_mod_count, and so on.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('task') .disableAutoSysFields() .insert({ description: 'example', priority: 1 });</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-disableWorkflow"> <h2 class="title topictitle2" id="ariaid-title8">GlideQuery - disableWorkflow()</h2> <div class="body refbody"><p class="shortdesc">Disables any business rules, flows, workflows, or audit records that would run or be created as the result of the query.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-disableWorkflow__table_wdb_5lc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 13. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1354">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1357">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1360">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1354 ">None</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1357 "> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1360 "> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-disableWorkflow__table_xdb_5lc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 14. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1395">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1398">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1395 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1398 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example updates multiple records in the Task table without triggering any automatic business processes.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('task') .disableWorkflow() .where('active', true) .updateMultiple({ priority: 1 });</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-forceUpdate"> <h2 class="title topictitle2" id="ariaid-title9">GlideQuery - forceUpdate()</h2> <div class="body refbody"><p class="shortdesc">Forces a database update even when no record changes are made. For example, you can use this method to force a business rule to execute.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-forceUpdate__table_rqj_zlc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 15. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1475">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1478">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1481">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1475 ">None</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1478 "> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1481 "> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-forceUpdate__table_sqj_zlc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 16. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1516">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1519">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1516 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1519 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example force updates Task records with a certain sys_id.</p> <pre class="pre codeblock"><code>var forceUpdate = new global.GlideQuery('task') .forceUpdate() .where('sys_id', taskId) .update()</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-get_S_O"> <h2 class="title topictitle2" id="ariaid-title10">GlideQuery - get(String key, Array selectedFields)</h2> <div class="body refbody"><p class="shortdesc">Returns a single record from the query.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-get_S_O__table_u1k_flc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 17. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1597">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1600">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1603">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1597 ">key</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1600 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1603 ">Sys_id of the record to return.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1597 ">selectedFields</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1600 ">Array</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1603 ">Optional. Additional fields to return in the result. <p class="p">Default: The system always returns the sys_id.</p> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-get_S_O__table_v1k_flc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 18. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1654">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1657">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1654 ">Optional</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1657 ">Object used to interact with a single record.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">Example that returns a record based on sys_id.</p> <pre class="pre codeblock"><code>var user = new global.GlideQuery('sys_user') .get('5137153cc611227c000bbd1bd8cd2005', ['first_name', 'last_name']) //Returns an Optional object. .orElse({ first_name: 'Default', last_name: 'User' }); //Method in the Optional class to return a default value. gs.info(JSON.stringify(user, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>{ "sys_id":"5137153cc611227c000bbd1bd8cd2005", "first_name":"Fred", "last_name":"Luddy" }</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-getBy_O_O"> <h2 class="title topictitle2" id="ariaid-title11">GlideQuery - getBy(Object keyValues, Array selectedFields)</h2> <div class="body refbody"><p class="shortdesc">Returns an Optional object containing a single record based on a set of name-value pairs to query by. Assumes the '=' operator for each name-value pair.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-getBy_O_O__table_ift_dlc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 19. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1740">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1743">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1746">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1740 ">keyValues</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1743 ">Object</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1746 ">Object where the keys are the name of the fields, and the values are the values to query for.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1740 ">selectedFields</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1743 ">Array</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1746 ">Optional. Additional fields to return in the result. <p class="p">Default: The system always returns the sys_id.</p> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-getBy_O_O__table_jft_dlc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 20. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1797">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1800">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1797 ">Optional</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1800 ">Object used to interact with a single record.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">Example that returns a record by querying for a user's name.</p> <pre class="pre codeblock"><code>var user = new global.GlideQuery('sys_user') .getBy({ first_name: 'Fred', last_name: 'Luddy' }, ['first_name', 'last_name', 'city', 'active']) // select first_name, last_name, city, active .orElse({ first_name: 'Nobody', last_name: 'Found', city: 'Nowhere', active: false }); gs.info(JSON.stringify(user, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>{ "first_name":"Fred", "last_name":"Luddy", "city":null, "active":true, "sys_id":"5137153cc611227c000bbd1bd8cd2005" }</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-groupBy_S"> <h2 class="title topictitle2" id="ariaid-title12">GlideQuery - groupBy(String fields)</h2> <div class="body refbody"><p class="shortdesc">Groups the query results by a designated field or fields.</p> <div class="section"> <p class="p">You must use this method with the <span class="keyword apiname">aggregate()</span> method.</p> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-groupBy_S__table_nww_pmc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 21. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1890">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1893">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1896">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1890 ">fields</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1893 ">String or Array of Strings</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1896 ">Field or fields to group the results by.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-groupBy_S__table_oww_pmc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 22. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1933">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e1936">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1933 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e1936 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example performs a query on the Task table that groups the records by priority, adds the numbers in the reassignment count field for each group, and returns groups with total reassignments greater than four.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('task') .where('active', true) //Returns the GlideQuery object to add more attributes to the query. .groupBy('priority') //Returns the GlideQuery object to add more attributes to the query. .aggregate('sum', 'reassignment_count') //Returns the GlideQuery object to add more attributes to the query. .having('sum', 'reassignment_count', '>', 4) //Returns the GlideQuery object to add more attributes to the query. .select() //Returns a stream of records wrapped in a Stream object. .toArray(10); //Terminal method in the Stream class that executes the query and returns the result. gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "group":{ "priority":1 }, "sum":{ "reassignment_count":11 } }, { "group":{ "priority":3 }, "sum":{ "reassignment_count":6 } }, { "group":{ "priority":5 }, "sum":{ "reassignment_count":5 } } ]</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-having_S_S_S_N"> <h2 class="title topictitle2" id="ariaid-title13">GlideQuery - having(String aggregateType, String field, String operator, Number value)</h2> <div class="body refbody"><p class="shortdesc">Filters aggregate groups so that you can display only groups of results that match a specified condition.</p> <div class="section"> <p class="p">Must use this method with the <span class="keyword apiname">aggregate()</span> or <span class="keyword apiname">groupBy()</span> methods.</p> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-having_S_S_S_N__table_jw3_vmc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 23. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2028">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2031">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2034">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2028 ">aggregateType</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2031 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2034 ">The type of aggregation function to perform. Options include:<div class="p"><ul class="ul" id="GQ-having_S_S_S_N__agg-type-options"><li class="li"><code class="ph codeph">min</code>: Returns the smallest value of all matching records.</li><li class="li"><code class="ph codeph">max</code>: Returns the largest value of all matching records.</li><li class="li"><code class="ph codeph">sum</code>: Returns the sum of all matching records.</li><li class="li"><code class="ph codeph">avg</code>: Returns the average of all matching records.</li><li class="li"><code class="ph codeph">count</code>: Returns the number of number of matching records.</li></ul> </div> </td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2028 ">field</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2031 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2034 ">Field on which to perform the operation.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2028 ">operator</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2031 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2034 ">Numeric operator to use in the operation. <div class="p">Options include:<ul class="ul"><li class="li"><code class="ph codeph">></code>: Greater than.</li><li class="li"><code class="ph codeph"><</code>: Less than.</li><li class="li"><code class="ph codeph">>=</code>: Greater than or equal to.</li><li class="li"><code class="ph codeph"><=</code>: Less than or equal to.</li><li class="li"><code class="ph codeph">=</code>: Equal to.</li><li class="li"><code class="ph codeph">!=</code>: Not equal to.</li></ul> </div> </td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2028 ">value</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2031 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2034 ">Number value to use in the operation.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-having_S_S_S_N__table_kw3_vmc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 24. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2170">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2173">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2170 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2173 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example performs a query on the Task table that groups the records by priority, adds the numbers in the reassignment count field for each group, and returns groups with total reassignments greater than four.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('task') .where('active', true) //Returns the GlideQuery object to add more attributes to the query. .groupBy('priority') //Returns the GlideQuery object to add more attributes to the query. .aggregate('sum', 'reassignment_count') //Returns the GlideQuery object to add more attributes to the query. .having('sum', 'reassignment_count', '>', 4) //Returns the GlideQuery object to add more attributes to the query. .select() //Returns a stream of records wrapped in a Stream object. .toArray(10); //Terminal method in the Stream class that executes the query and returns the result. gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "group":{ "priority":1 }, "sum":{ "reassignment_count":11 } }, { "group":{ "priority":3 }, "sum":{ "reassignment_count":6 } }, { "group":{ "priority":5 }, "sum":{ "reassignment_count":5 } } ]</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-insert_O_O"> <h2 class="title topictitle2" id="ariaid-title14">GlideQuery - insert(Object keyValues, Object selectedFields)</h2> <div class="body refbody"><p class="shortdesc">Inserts a record and returns an Optional object containing the record.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-insert_O_O__table_m5f_hlc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 25. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2257">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2260">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2263">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2257 ">keyValues</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2260 ">Object</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2263 ">Object containing name-value pairs to insert into the record. Unspecified fields will be null.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2257 ">selectedFields</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2260 ">Array</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2263 ">Optional. Additional fields to return in the result. <p class="p">Default: The system always returns the sys_id.</p> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-insert_O_O__table_n5f_hlc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 26. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2314">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2317">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2314 ">Optional</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2317 ">Object used to interact with a single record.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to insert a record based on a user's first and last name.</p> <pre class="pre codeblock"><code>var fred = new global.GlideQuery('sys_user') .insert({ first_name: 'Fred', last_name: 'Luddy' }) .get(); gs.info(JSON.stringify(fred, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>{ "sys_id":"cf16eed0e82a9010f8778bda83d255d2", "first_name":"Fred", "last_name":"Luddy" }</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-insertOrUpdate_O_O"> <h2 class="title topictitle2" id="ariaid-title15">GlideQuery - insertOrUpdate(Object changes, Object selectedFields)</h2> <div class="body refbody"><p class="shortdesc">Updates an existing record, or inserts a new record if one does not already exist.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-insertOrUpdate_O_O__table_sdt_3lc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 27. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2400">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2403">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2406">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2400 ">changes</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2403 ">Object</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2406 ">Object containing name-value pairs to update or insert into the record.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2400 ">selectedFields</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2403 ">Array</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2406 ">Optional. Additional fields to return in the result. <p class="p">Default: The system always returns the sys_id.</p> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-insertOrUpdate_O_O__table_tdt_3lc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 28. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2457">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2460">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2457 ">Optional</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2460 ">Object used to interact with a single record.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to insert a new record that does not already exist in the system.</p> <pre class="pre codeblock"><code>// insert a new record var user = new GlideQuery('sys_user') .insertOrUpdate({ first_name: 'George', last_name: 'Griffey' }) .orElse(null);</code></pre> </div> <div class="example"> <p class="p">This example shows how to update an existing record.</p> <pre class="pre codeblock"><code>// update existing record var user = new global.GlideQuery('sys_user') .insertOrUpdate({ sys_id: '2d0efd6c73662300bb513198caf6a72e', first_name: 'George', last_name: 'Griffey' }) .orElse(null);</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-limit_N"> <h2 class="title topictitle2" id="ariaid-title16">GlideQuery - limit(Number limit)</h2> <div class="body refbody"><p class="shortdesc">Limits the number of records returned in a query.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-limit_N__table_gvh_2mc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 29. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2547">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2550">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2553">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2547 ">limit</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2550 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2553 ">Number of records to return.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-limit_N__table_hvh_2mc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 30. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2590">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2593">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2590 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2593 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to limit the results returned to five records.</p> <pre class="pre codeblock"><code>var incidents = new global.GlideQuery('incident') .limit(5) .select('priority', 'description') .toArray(100); gs.info(JSON.stringify(incidents, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "priority":3, "description":"I am unable to connect to the email server. It appears to be down.", "sys_id":"1c741bd70b2322007518478d83673af3" }, { "priority":3, "description":"My computer is not detecting the headphone device. It could be an issue with the USB port.", "sys_id":"1c832706732023002728660c4cf6a7b9" }, { "priority":1, "description":"I can't remember my password and need to log in. Can someone reset my password asap? I am blocked on several urgent items until I can log in again.", "sys_id":"46b66a40a9fe198101f243dfbc79033d" }, { "priority":4, "description":"Currently running 10GR1 and need to upgrade to 10GR2.", "sys_id":"46b9490da9fe1981003c938dab89bda3" }, { "priority":3, "description":"I'm replacing my old phone with a Blackberry and require assistance to get it set up. I'd like to get the files and contacts transferred to the new phone and need help getting the two factor authentication app set up on it.", "sys_id":"46c03489a9fe19810148cd5b8cbf501e" } ]</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-max_S"> <h2 class="title topictitle2" id="ariaid-title17">GlideQuery - max(String field)</h2> <div class="body refbody"><p class="shortdesc">Returns the aggregate maximum of a given field.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-max_S__table_b2q_jmc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 31. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2676">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2679">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2682">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2676 ">field</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2679 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2682 ">Field on which to perform the operation.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-max_S__table_c2q_jmc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 32. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2719">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2722">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2719 ">Optional</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2722 ">Object used to interact with a single record.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to return the maximum value, or highest alphanumeric value, of a given field.</p> <pre class="pre codeblock"><code>var name = new global.GlideQuery('sys_user') .max('last_name') .orElse(''); gs.info(JSON.stringify(name));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>"Zortman"</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-min_S"> <h2 class="title topictitle2" id="ariaid-title18">GlideQuery - min(String field)</h2> <div class="body refbody"><p class="shortdesc">Returns the aggregate minimum of a given field.</p> <div class="section" id="GQ-min_S__section_uq3_kqb_pmb"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-min_S__table_b2q_jmc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 33. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2806">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2809">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2812">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2806 ">field</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2809 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2812 ">Field on which to perform the operation.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-min_S__table_c2q_jmc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 34. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2849">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2852">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2849 ">Optional</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2852 ">Object used to interact with a single record.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to return the minimum value, or lowest alphanumeric value, of a given field.</p> <pre class="pre codeblock"><code>var name = new global.GlideQuery('sys_user') .min('last_name') .orElse(''); gs.info(JSON.stringify(name));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>"Abel"</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-orderBy_S"> <h2 class="title topictitle2" id="ariaid-title19">GlideQuery - orderBy(String fields)</h2> <div class="body refbody"><p class="shortdesc">Orders the returned result in ascending order by a given field.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-orderBy_S__table_gkw_1mc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 35. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2935">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2938">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2941">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2935 ">fields</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2938 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2941 ">Comma-delimited fields to order the result by in ascending order.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-orderBy_S__table_hkw_1mc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 36. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2978">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e2981">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2978 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e2981 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to order results in ascending order by record number.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('incident') .orderBy('number') .limit(5) .select('priority', 'description') //Returns a stream of records wrapped in a Stream object. .toArray(100); //Terminal method in the Stream class that executes the query and returns the result. gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "priority":1, "description":"User can't access email on mail.company.com.\n\t\t", "sys_id":"9c573169c611228700193229fff72400" }, { "priority":1, "description":"User can't get to any of his files on the file server.", "sys_id":"9d385017c611228701d22104cc95c371" }, { "priority":1, "description":"I just moved from floor 2 to floor 3 and my laptop cannot connect to any wireless network.", "sys_id":"e8caedcbc0a80164017df472f39eaed1" }, { "priority":1, "description":"User forgot their email password.", "sys_id":"9d3c1197c611228701cd1d94bc32d76d" }, { "priority":1, "description":"Watcher daemon detected that the CPU was 100% busy for more than 10 minutes", "sys_id":"e8e875b0c0a80164009dc852b4d677d5" }</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-orderByDesc_S_S"> <h2 class="title topictitle2" id="ariaid-title20">GlideQuery - orderByDesc(String fieldOrAggregate, String field)</h2> <div class="body refbody"><p class="shortdesc">Orders the returned result in descending order by a given field.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-orderByDesc_S_S__table_yxt_cmc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 37. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3065">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3068">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3071">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3065 ">fieldOrAggregate</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3068 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3071 ">If the query does not use the <span class="keyword apiname">aggregate()</span> method, pass the field to order the results by. <p class="p">If the query uses the <span class="keyword apiname">aggregate()</span> method, pass the type of aggregation function to perform.</p> <div class="p">Options include:<ul class="ul" id="GQ-orderByDesc_S_S__ul_lzt_ggh_pmb"><li class="li"><code class="ph codeph">min</code>: Returns the smallest value of all matching records.</li><li class="li"><code class="ph codeph">max</code>: Returns the largest value of all matching records.</li><li class="li"><code class="ph codeph">sum</code>: Returns the sum of all matching records.</li><li class="li"><code class="ph codeph">avg</code>: Returns the average of all matching records.</li><li class="li"><code class="ph codeph">count</code>: Returns the number of number of matching records.</li></ul> </div> </td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3065 ">field</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3068 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3071 ">Optional. Field to order the result by in descending order. Required for queries using the <span class="keyword apiname">aggregate()</span> method.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-orderByDesc_S_S__table_zxt_cmc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 38. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3161">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3164">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3161 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3164 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to order the result in descending order by number.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('incident') .orderByDesc('number') .select('number', 'description') .limit(5) .toArray(100); gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "number":"INC0010112", "description":null, "sys_id":"552c48888c033300964f4932b03eb092" }, { "number":"INC0010111", "description":null, "sys_id":"a83820b58f723300e7e16c7827bdeed2" }, { "number":"INC0009009", "description":"Unable to access the shared folder. Please provide access.", "sys_id":"57af7aec73d423002728660c4cf6a71c" }, { "number":"INC0009005", "description":"Unable to send or receive emails.", "sys_id":"ed92e8d173d023002728660c4cf6a7bc" }, { "number":"INC0009004", "description":"While launching the defect tracking base URL, it is redirecting to an error page.", "sys_id":"e329de99731423002728660c4cf6a73c" } ]</code></pre> </div> <div class="example"> <p class="p">This example shows how to order an aggregate result by the sum of child incidents.</p> <pre class="pre codeblock"><code>var aggQuery = new GlideQuery('incident') .aggregate('sum', 'child_incidents') .groupBy('category') .orderByDesc('sum', 'child_incidents') .select() .toArray(100); gs.info(JSON.stringify(aggQuery, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "group":{ "category":"hardware" }, "sum":{ "child_incidents":2 } }, { "group":{ "category":"inquiry" }, "sum":{ "child_incidents":1 } }, { "group":{ "category":"software" }, "sum":{ "child_incidents":0 } }, { "group":{ "category":"" }, "sum":{ "child_incidents":null } }, { "group":{ "category":"database" }, "sum":{ "child_incidents":null } }, { "group":{ "category":"network" }, "sum":{ "child_incidents":null } } ]</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-orWhere_S_S"> <h2 class="title topictitle2" id="ariaid-title21">GlideQuery - orWhere(String fieldOrQuery, String operator, Any value)</h2> <div class="body refbody"><p class="shortdesc">Adds an OR clause to a query that returns values based on a given condition.</p> <div class="section"> <div class="p"><div class="note"><span class="notetitle">Note:</span> Precede this method with the <span class="keyword apiname">where()</span>, <span class="keyword apiname">whereNull()</span>, or <span class="keyword apiname">whereNotNull()</span> methods.</div> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-orWhere_S_S__table_jf5_vkc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 39. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3275">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3278">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3281">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3275 ">fieldOrQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3278 ">String or <a class="xref" href="GlideQueryGlobalAPI.html" title="The GlideQuery API is an alternative to the GlideRecord API to perform CRUD operations on record data from server-side scripts.">GlideQuery</a></td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3281 ">Field or another GlideQuery object used in the where clause. If passing a field, you can dot-walk to a desired value. For example, <code class="ph codeph">'company.name'</code>.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3275 ">operator</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3278 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3281 ">Optional. Operator used in the OR clause. If you do not pass an argument, the system uses the = operator. You do not need to include a placeholder value.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3275 ">value</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3278 ">Any</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3281 ">Value used in the OR clause.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-orWhere_S_S__table_kf5_vkc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 40. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3357">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3360">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3357 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3360 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to add a simple OR clause to a query.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('sys_user') .where('failed_attempts', '>', 0) .orWhere('last_login', '<', '2019-04-15') .select() .toArray(100) gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "sys_id":"005d500b536073005e0addeeff7b12f4" }, { "sys_id":"30ad318577ab2300454792718a10619e" }, { "sys_id":"3883f4c0730123002728660c4cf6a754" }, { "sys_id":"3988a3ca732023002728660c4cf6a757" }, { "sys_id":"4ac73ecd738123002728660c4cf6a72c" }, { "sys_id":"8ff5b254b33213005e3de13516a8dcf7" }, { "sys_id":"d999e5fc77e72300454792718a10611d" } ]</code></pre> </div> <div class="example"> <p class="p">This example shows how to add a <code class="ph codeph">orWhere</code> clause that contains a separate query.</p> <pre class="pre codeblock"><code>// active = true OR (title = 'Vice President' AND state = 'CA') var query = new GlideQuery('sys_user') .where('active', true) .orWhere(new GlideQuery() .where('title', 'Vice President') .where('state', 'CA')) .select('name') .limit(5) .toArray(100) gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "name":"survey user", "sys_id":"005d500b536073005e0addeeff7b12f4" }, { "name":"Lucius Bagnoli", "sys_id":"02826bf03710200044e0bfc8bcbe5d3f" }, { "name":"Jimmie Barninger", "sys_id":"02826bf03710200044e0bfc8bcbe5d55" }, { "name":"Melinda Carleton", "sys_id":"02826bf03710200044e0bfc8bcbe5d5e" }, { "name":"Jewel Agresta", "sys_id":"02826bf03710200044e0bfc8bcbe5d64" } ]</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-orWhereNotNull_S"> <h2 class="title topictitle2" id="ariaid-title22">GlideQuery - orWhereNotNull(String field)</h2> <div class="body refbody"><p class="shortdesc">Adds an OR clause that returns records that do not contain a null value in a given field.</p> <div class="section"> <div class="p"><div class="note"><span class="notetitle">Note:</span> Precede this method with the <span class="keyword apiname">where()</span>, <span class="keyword apiname">whereNull()</span>, or <span class="keyword apiname">whereNotNull()</span> methods.</div> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-orWhereNotNull_S__table_wpk_zkc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 41. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3475">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3478">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3481">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3475 ">field</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3478 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3481 ">Field used in the query.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-orWhereNotNull_S__table_xpk_zkc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 42. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3518">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3521">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3518 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3521 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to query the User table and return results where the first and last names are not null.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('sys_user') .whereNotNull('first_name') .orWhereNotNull('last_name') .select() .limit(5) .toArray(100) gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "sys_id":"005d500b536073005e0addeeff7b12f4" }, { "sys_id":"02826bf03710200044e0bfc8bcbe5d3f" }, { "sys_id":"02826bf03710200044e0bfc8bcbe5d55" }, { "sys_id":"02826bf03710200044e0bfc8bcbe5d5e" }, { "sys_id":"02826bf03710200044e0bfc8bcbe5d64" } ]</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-orWhereNull_S"> <h2 class="title topictitle2" id="ariaid-title23">GlideQuery - orWhereNull(String field)</h2> <div class="body refbody"><p class="shortdesc">Adds an OR clause to a query that returns records that contain a null value in a given field.</p> <div class="section"> <div class="p"><div class="note"><span class="notetitle">Note:</span> Precede this method with the <span class="keyword apiname">where()</span>, <span class="keyword apiname">whereNull()</span>, or <span class="keyword apiname">whereNotNull()</span> methods.</div> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-orWhereNull_S__table_xll_clc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 43. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3617">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3620">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3623">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3617 ">field</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3620 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3623 ">Field used in the query.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-orWhereNull_S__table_yll_clc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 44. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3660">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3663">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3660 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3663 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to query the User table and return records where the first or last names are null.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('sys_user') .whereNull('last_name') .orWhereNull('first_name') .select() .toArray(100) gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "sys_id":"1195569be81a1010f8778bda83d25585" }, { "sys_id":"5136503cc611227c0183e96598c4f706" } ]</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-parse_S_S"> <h2 class="title topictitle2" id="ariaid-title24">GlideQuery - parse(String table, String encoded_query )</h2> <div class="body refbody"><p class="shortdesc">Adds an encoded query to a new <span class="keyword apiname">GlideQuery</span> query.</p> <div class="section"> <p class="p">This method does not support all <span class="keyword apiname">GlideRecord</span> encoded query operators. The following operators are currently supported:</p> <div class="p"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" class="table" frame="border" border="1" rules="all"><colgroup><col style="width:13.623978201634879%" /><col style="width:23.29700272479564%" /><col style="width:31.471389645776572%" /><col style="width:31.607629427792915%" /></colgroup><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">=</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">ANYTHING</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">GT_FIELD</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">NOT IN</code></td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">!=</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">BETWEEN</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">GT_OR_EQUALS_FIELD</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">NOT LIKE</code></td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">></code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">CONTAINS</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">IN</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">NSAMEAS</code></td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">>=</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">DOES NOT CONTAIN</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">INSTANCEOF</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">ON</code></td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph"><</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">DYNAMIC</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">LIKE</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">SAMEAS</code></td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph"><=</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">EMPTYSTRING</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">LT_FIELD</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">STARTSWITH</code></td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;"> </td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">ENDSWITH</code></td><td class="entry cellrowborder" style="vertical-align:top;"><code class="ph codeph">LT_OR_EQUALS_FIELD</code></td><td class="entry cellrowborder" style="vertical-align:top;"> </td></tr></tbody></table> </div> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-parse_S_S__table_uw4_rcx_brb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 45. </span>Parameters</span></caption><colgroup><col style="width:17.94478527607362%" /><col style="width:15.337423312883436%" /><col style="width:66.71779141104295%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3904">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3907">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3910">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3904 ">table</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3907 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3910 ">Table to query, such as task or incident.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3904 ">encoded_query</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3907 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3910 "><a class="xref" href="../use/using-lists/concept/c_EncodedQueryStrings.html" target="_blank" rel="noopener noreferrer">Encoded query</a> to apply to the records in the specified table.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-parse_S_S__table_vw4_rcx_brb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 46. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3962">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e3965">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3962 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e3965 ">GlideQuery object containing the encoded query.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">The following example creates a GlideQuery object that contains all active records in the task table, ordered by the priority field, returned as a Stream object that contains just the description field.</p> <pre class="pre codeblock"><code>GlideQuery.parse('task', 'active=true^ORDERBYpriority') // Pass the encoded query to use to parse the Task records .select('description') // Return the records that match the encoded query .forEach(doSomething); // Do some processing on each of the returned records</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-select_S"> <h2 class="title topictitle2" id="ariaid-title25">GlideQuery - select(String fields)</h2> <div class="body refbody"><p class="shortdesc">Returns the results of the query as a Stream object containing the specified fields.</p> <div class="section"> <div class="p"><div class="note"><span class="notetitle">Note:</span> Use a terminal method in the <span class="keyword apiname">Stream</span> class to get the result of the query. For more information, see <a class="xref" href="../../Stream/concept/StreamGlobalAPI.html" title="The Stream API interacts with a stream of items such as records. For example, you can use the forEach() method to update the state of each record in a stream returned by the GlideQuery API.">Stream</a>.</div> </div> <p class="p">You can append a flag to a field name to return the field's metadata instead of the field's value. For example, using the field name <code class="ph codeph">company$DISPLAY</code> returns the display value of a company field. Possible flags include:</p> <div class="p"><ul class="ul" id="GQ-select_S__ul_u52_b3l_qmb"><li class="li"><code class="ph codeph">DISPLAY</code>: Returns the display value of a field.</li><li class="li"><code class="ph codeph">CURRENCY_CODE</code>: Returns the currency code of a currency field. For example, <code class="ph codeph">USD</code>.</li><li class="li"><code class="ph codeph">CURRENCY_DISPLAY</code>: Returns the currency display value of a currency field. For example, <code class="ph codeph">¥123.45</code>.</li><li class="li"><code class="ph codeph">CURRENCY_STRING</code>: Returns the currency string of a currency field. For example, <code class="ph codeph">JPY;123.45</code>.</li></ul> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-select_S__table_ktr_qlc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 47. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4104">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4107">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4110">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4104 ">fields</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4107 ">String or Array of Strings</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4110 ">Optional. Fields to display in the result. You can provide any number of fields as arguments, dot-walk to a desired value, or use a flag. For example: <pre class="pre codeblock"><code>select('first_name', 'location.city', 'company$DISPLAY');</code></pre>or<pre class="pre codeblock"><code>select(['first_name', 'location.city', 'company$DISPLAY']);</code></pre> <p class="p">Default: The system always returns the sys_id.</p> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-select_S__table_ltr_qlc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 48. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4155">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4158">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4155 ">Stream</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4158 ">Object used to interact with a stream of items such as records.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to select fields to display from the query and use <code class="ph codeph">$DISPLAY</code> to return the display value of a field.</p> <pre class="pre codeblock"><code>var stream = new global.GlideQuery('sys_user') .select('first_name', 'last_name', 'company$DISPLAY') .toArray(5); gs.info(JSON.stringify(stream, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "first_name":"survey", "last_name":"user", "company$DISPLAY":"", "sys_id":"005d500b536073005e0addeeff7b12f4" }, { "first_name":"Lucius", "last_name":"Bagnoli", "company$DISPLAY":"ACME Japan", "sys_id":"02826bf03710200044e0bfc8bcbe5d3f" }, { "first_name":"Jimmie", "last_name":"Barninger", "company$DISPLAY":"ACME South America", "sys_id":"02826bf03710200044e0bfc8bcbe5d55" }, { "first_name":"Melinda", "last_name":"Carleton", "company$DISPLAY":"ACME UK", "sys_id":"02826bf03710200044e0bfc8bcbe5d5e" }, { "first_name":"Jewel", "last_name":"Agresta", "company$DISPLAY":"ACME UK", "sys_id":"02826bf03710200044e0bfc8bcbe5d64" } ]</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-selectOne_S"> <h2 class="title topictitle2" id="ariaid-title26">GlideQuery - selectOne(String fields)</h2> <div class="body refbody"><p class="shortdesc">Returns the result of the query as an Optional object containing specified fields.</p> <div class="section"> <p class="p">Use this method when returning a single record, or to test if a record exists. If returning multiple records, use the <span class="keyword apiname">select()</span> method to return a Stream object.</p> <p class="p">You can append a flag to a field name to return the field's metadata instead of the field's value. For example, using the field name <code class="ph codeph">company$DISPLAY</code> returns the display value of a company field. Possible flags include:</p> <div class="p"><ul class="ul" id="GQ-selectOne_S__ul_u52_b3l_qmb"><li class="li"><code class="ph codeph">DISPLAY</code>: Returns the display value of a field.</li><li class="li"><code class="ph codeph">CURRENCY_CODE</code>: Returns the currency code of a currency field. For example, <code class="ph codeph">USD</code>.</li><li class="li"><code class="ph codeph">CURRENCY_DISPLAY</code>: Returns the currency display value of a currency field. For example, <code class="ph codeph">¥123.45</code>.</li><li class="li"><code class="ph codeph">CURRENCY_STRING</code>: Returns the currency string of a currency field. For example, <code class="ph codeph">JPY;123.45</code>.</li></ul> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-selectOne_S__table_pw3_slc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 49. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4290">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4293">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4296">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4290 ">fields</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4293 ">String or Array of Strings</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4296 ">Optional. Fields to display in the result. You can provide any number of fields as arguments, dot-walk to a desired value, or use a flag. For example: <pre class="pre codeblock"><code>selectOne('first_name', 'location.city', 'company$DISPLAY');</code></pre>or<pre class="pre codeblock"><code>selectOne(['first_name', 'location.city', 'company$DISPLAY']);</code></pre> <p class="p">Default: The system always returns the sys_id.</p> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-selectOne_S__table_qw3_slc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 50. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4341">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4344">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4341 ">Optional</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4344 ">Object used to interact with a single record.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to return a single record as an Optional object and display specified fields.</p> <pre class="pre codeblock"><code>var user = new global.GlideQuery('sys_user') .where('zip', '12345') .whereNotNull('last_name') .selectOne('first_name', 'last_name', 'company$DISPLAY') .get(); gs.info(JSON.stringify(user, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>{ "first_name":"Abel", "last_name":"Tuter", "company$DISPLAY":"ACME South America", "sys_id":"62826bf03710200044e0bfc8bcbe5df1" }</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-sum_S"> <h2 class="title topictitle2" id="ariaid-title27">GlideQuery - sum(String field)</h2> <div class="body refbody"><p class="shortdesc">Returns the aggregate sum of a given numeric field.</p> <div class="section"> <div class="p">You can only use this method on fields of the following types:<ul class="ul" id="GQ-sum_S__ul_ikp_2wl_qmb"><li class="li">Integer</li><li class="li">Long</li><li class="li">Floating Point Number</li><li class="li">Double</li><li class="li">Currency</li></ul> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-sum_S__table_tfz_mmc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 51. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4447">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4450">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4453">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4447 ">field</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4450 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4453 ">Field on which to perform the operation.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-sum_S__table_ufz_mmc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 52. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4490">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4493">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4490 ">Optional</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4493 ">Object used to interact with a single record.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to return the sum of all faults in the cmdb_ci table.</p> <pre class="pre codeblock"><code>var totalFaults = new global.GlideQuery('cmdb_ci') .sum('fault_count') .orElse(0); gs.info(JSON.stringify(totalFaults));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>10</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-toGlideRecord"> <h2 class="title topictitle2" id="ariaid-title28">GlideQuery - toGlideRecord()</h2> <div class="body refbody"><p class="shortdesc">Returns a GlideRecord object that represents the current query. Returns a GlideAggregrate object if the query uses the <span class="keyword apiname">GlideQuery.aggregate()</span> method.</p> <div class="section"> <p class="p">After transforming the query, use the <span class="keyword apiname">query()</span> method in the <a class="xref" href="../../glideRecordScoped/concept/c_GlideRecordScopedAPI.html" title="Scoped GlideRecord is used for database operations.">GlideRecord</a> or <a class="xref" href="../../glideAggregateScoped/concept/c_GlideAggregateScopedAPI.html" title="GlideAggregate enables creating database aggregation queries.">GlideAggregate</a> classes to query the database.</p> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-toGlideRecord__table_cgy_wmc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 53. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4607">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4610">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4613">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4607 ">None</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4610 "> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4613 "> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-toGlideRecord__table_dgy_wmc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 54. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4648">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4651">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4648 ">GlideRecord or GlideAggregate</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4651 ">GlideRecord object that contains the query. If you used the <span class="keyword apiname">GlideQuery.aggregate()</span> method, then the method returns a GlideAggregrate object instead.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to transform a GlideQuery object into a GlideRecord.</p> <pre class="pre codeblock"><code>var userGr = new global.GlideQuery('sys_user') .where('active', true) .whereNotNull('first_name') .limit(10) .toGlideRecord(); userGr.query();</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-update_O_O"> <h2 class="title topictitle2" id="ariaid-title29">GlideQuery - update(Object changes, Object selectedFields)</h2> <div class="body refbody"><p class="shortdesc">Updates an existing record that matches the defined conditions.</p> <div class="section"> <p class="p">Before using this method, call the <span class="keyword apiname">where()</span> method to specify the conditions that a record must meet to be updated.</p> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-update_O_O__table_cbg_klc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 55. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4737">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4740">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4743">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4737 ">changes</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4740 ">Object</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4743 ">Object containing name-value pairs to update in the record. Names must match fields in the table.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4737 ">selectedFields</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4740 ">Array</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4743 ">Optional. Additional fields to return in the result. <p class="p">Default: The system always returns the sys_id.</p> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-update_O_O__table_dbg_klc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 56. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4794">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4797">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4794 ">Optional</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4797 ">Object used to interact with a single record.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to update a record with new values.</p> <pre class="pre codeblock"><code>var updateRecord = new global.GlideQuery('sys_user') .where('sys_id', '0a826bf03710200044e0bfc8bcbe5d7a') .update({ city: 'Los Angeles' });</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-updateMultiple_O"> <h2 class="title topictitle2" id="ariaid-title30">GlideQuery - updateMultiple(Object changes)</h2> <div class="body refbody"><p class="shortdesc">Updates all existing records that match the defined conditions. Returns the number of records updated.</p> <div class="section"> <p class="p">Before using this method, call the <span class="keyword apiname">where()</span> method to specify the conditions that the records must meet to be updated.</p> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-updateMultiple_O__table_ywr_llc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 57. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4881">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4884">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4887">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4881 ">changes</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4884 ">Object</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4887 ">Object containing name-value pairs to update in the record. Names must match fields in the table.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-updateMultiple_O__table_zwr_llc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 58. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4924">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e4927">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4924 ">Object</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e4927 ">Object containing the number of records that were updated. Keys include: <ul class="ul" id="GQ-updateMultiple_O__ul_epc_mxw_wmb"><li class="li"><code class="ph codeph">rowCount</code>: Number of rows updated in the table.</li></ul> </td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to update any records that fit a defined criteria.</p> <pre class="pre codeblock"><code>var update = new global.GlideQuery('sys_user') .where('active', false) .where('last_name', 'Griffey') .updateMultiple({ active: true }); gs.info(JSON.stringify(update));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>{"rowCount":1}</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-where_S_S"> <h2 class="title topictitle2" id="ariaid-title31">GlideQuery - where(String fieldOrQuery, String operator, Any value)</h2> <div class="body refbody"><p class="shortdesc">Adds a Where clause to the query that returns values based on a given condition.</p> <div class="section"> <div class="p"><div class="note"><span class="notetitle">Note:</span> Do not precede this method with the <span class="keyword apiname">orWhere()</span>, <span class="keyword apiname">orWhereNull()</span>, or <span class="keyword apiname">orWhereNotNull()</span> methods.</div> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-where_S_S__table_jf5_vkc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 59. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5030">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5033">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5036">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5030 ">fieldOrQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5033 ">String or <a class="xref" href="GlideQueryGlobalAPI.html" title="The GlideQuery API is an alternative to the GlideRecord API to perform CRUD operations on record data from server-side scripts.">GlideQuery</a></td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5036 ">Field or another GlideQuery object used in the where clause. If passing a field, you can dot-walk to a desired value. For example, <code class="ph codeph">'company.name'</code>.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5030 ">operator</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5033 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5036 ">Optional. Operator used in the where clause. If you do not pass an argument, the system uses the = operator.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5030 ">value</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5033 ">Any</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5036 ">Value used in the where clause.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-where_S_S__table_n5k_5kc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 60. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5112">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5115">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5112 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5115 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to return records from the User table where active is true and the user last logged on after a specified date.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('sys_user') .where('active', true) .where('last_login', '>', '2016-04-15') .limit(5) .select() .toArray(100) gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "sys_id":"b0f31e5673500010c2e7660c4cf6a711" }, { "sys_id":"8ff5b254b33213005e3de13516a8dcf7" }, { "sys_id":"d999e5fc77e72300454792718a10611d" }, { "sys_id":"30ad318577ab2300454792718a10619e" }, { "sys_id":"3883f4c0730123002728660c4cf6a754" } ]</code></pre> </div> <div class="example"> <p class="p">This example shows how to return records from the Incident table where active is true and where either the priority or the severity is 1.</p> <pre class="pre codeblock"><code>// active = true AND (priority = 1 OR severity = 1) var query = new GlideQuery('incident') .where('active', true) .where(new GlideQuery() .where('priority', 1) .orWhere('severity', 1)) .limit(5) .select() .toArray(100) gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "sys_id":"b0f31e5673500010c2e7660c4cf6a711" }, { "sys_id":"8ff5b254b33213005e3de13516a8dcf7" }, { "sys_id":"d999e5fc77e72300454792718a10611d" }, { "sys_id":"30ad318577ab2300454792718a10619e" }, { "sys_id":"3883f4c0730123002728660c4cf6a754" } ]</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-whereNotNull_S"> <h2 class="title topictitle2" id="ariaid-title32">GlideQuery - whereNotNull(String field)</h2> <div class="body refbody"><p class="shortdesc">Returns records that do not contain a null value in a given field.</p> <div class="section"> <div class="p"><div class="note"><span class="notetitle">Note:</span> Do not precede this method with the <span class="keyword apiname">orWhere()</span>, <span class="keyword apiname">orWhereNull()</span>, or <span class="keyword apiname">orWhereNotNull()</span> methods.</div> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-whereNotNull_S__table_bfj_xkc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 61. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5227">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5230">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5233">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5227 ">field</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5230 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5233 ">Field used in the query.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-whereNotNull_S__table_cfj_xkc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 62. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5270">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5273">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5270 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5273 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to query the User table and return results where the first_name field is not null.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('sys_user') .whereNotNull('first_name') .select() .limit(5) .toArray(100) gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "sys_id":"005d500b536073005e0addeeff7b12f4" }, { "sys_id":"02826bf03710200044e0bfc8bcbe5d3f" }, { "sys_id":"02826bf03710200044e0bfc8bcbe5d55" }, { "sys_id":"02826bf03710200044e0bfc8bcbe5d5e" }, { "sys_id":"02826bf03710200044e0bfc8bcbe5d64" } ]</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-whereNull_S"> <h2 class="title topictitle2" id="ariaid-title33">GlideQuery - whereNull(String field)</h2> <div class="body refbody"><p class="shortdesc">Returns records that contain a null value in a given field.</p> <div class="section"> <div class="p"><div class="note"><span class="notetitle">Note:</span> Do not precede this method with the <span class="keyword apiname">orWhere()</span>, <span class="keyword apiname">orWhereNull()</span>, or <span class="keyword apiname">orWhereNotNull()</span> methods.</div> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-whereNull_S__table_cqv_1lc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 63. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5369">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5372">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5375">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5369 ">field</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5372 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5375 ">Field used in the query.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-whereNull_S__table_dqv_1lc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 64. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5412">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5415">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5412 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5415 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to query the User table and return records where the first or last names are null.</p> <pre class="pre codeblock"><code>var query = new global.GlideQuery('sys_user') .whereNull('last_name') .orWhereNull('first_name') .select() .toArray(100) gs.info(JSON.stringify(query, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "sys_id":"1195569be81a1010f8778bda83d25585" }, { "sys_id":"5136503cc611227c0183e96598c4f706" } ]</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="GQ-withAcls"> <h2 class="title topictitle2" id="ariaid-title34">GlideQuery - withAcls()</h2> <div class="body refbody"><p class="shortdesc">Executes the query using the <span class="keyword apiname">GlideRecordSecure</span> API to securely query the database while honoring ACLs.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-withAcls__table_edy_fmc_nmb" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 65. </span>Parameters</span></caption><colgroup><col style="width:16.666666666666664%" /><col style="width:16.666666666666664%" /><col style="width:66.66666666666666%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5502">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5505">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5508">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5502 ">None</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5505 "> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5508 "> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="GQ-withAcls__table_fdy_fmc_nmb" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 66. </span>Returns</span></caption><colgroup><col style="width:25%" /><col style="width:75%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5543">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d769195e5546">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5543 ">GlideQuery</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d769195e5546 ">The query object being built.</td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">This example shows how to execute a secure query using ACLs.</p> <pre class="pre codeblock"><code>var users = new global.GlideQuery('sys_user') .withAcls() .limit(5) .orderByDesc('first_name') .select('first_name') .toArray(100); gs.info(JSON.stringify(users, null, 2));</code></pre> <p class="p">Output:</p> <pre class="pre codeblock"><code>[ { "first_name":"Zane", "sys_id":"16826bf03710200044e0bfc8bcbe5dbc" }, { "first_name":"Zackary", "sys_id":"8a826bf03710200044e0bfc8bcbe5d69" }, { "first_name":"Yvette", "sys_id":"4e826bf03710200044e0bfc8bcbe5d57" }, { "first_name":"Winnie", "sys_id":"f682abf03710200044e0bfc8bcbe5d1d" }, { "first_name":"Wilmer", "sys_id":"42826bf03710200044e0bfc8bcbe5d7b" } ]</code></pre> </div> </div> </div> </div> </body></html></div>