{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":809,"path":"api-reference/server-api-reference/GlideQueryGlobalAPI.md","title":"GlideQuery - Scoped, Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# GlideQuery - Scoped, Global\n\nThe GlideQuery script include is an alternative to the GlideRecord API for performing CRUD operations on record data from server-side scripts.\n\nThe GlideQuery script include lets you:\n\n-   Use standard JavaScript objects and types for queries and results.\n-   Quickly diagnose query errors with additional checks and clear error messages.\n-   Simplify your code by avoiding boiler-plate query patterns.\n-   Avoid common performance issues without needing deeper knowledge of the GlideRecord.\n\nUse the GlideQuery script include in scoped or global server-side scripts. When used within a scoped app, it must be prefixed with the global scope.\n\n```\nnew global.GlideQuery('sys_user')\n// ...\n```\n\nThis script include requires the GlideQuery \\[com.sn\\_glidequery\\] plugin.\n\n## Implementation\n\nThis script include works together with the [Stream](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/StreamGlobalAPI.md) and [Optional](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/OptionalGlobalAPI.md) APIs in a 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.\n\nIf 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.\n\nFor example, this script performs a query on the Task table, groups the records by priority, and returns each priority that has total reassignments greater than four.\n\n```\nvar query = new global.GlideQuery('task')\n    .where('active', true) //Returns new GlideQuery object with a \"where\" clause.\n    .groupBy('priority') //Returns new GlideQuery object with a \"group by\" clause.\n    .aggregate('sum', 'reassignment_count') //Returns new GlideQuery object with a \"sum(reassignment_count)\" clause.\n    .having('sum', 'reassignment_count', '>', 4) //Returns new GlideQuery object with a \"having reassignment_count > 4\" clause.\n    .select() //Returns a stream of records wrapped in a Stream object.  \n    .forEach(function (priority){ //Terminal method in the Stream class that executes the query and returns the result. \n      gs.info(\"Priority \" + priority.group.priority + \": \" + priority.sum.reassignment_count + \" reassignments\");\n    });\n```\n\nOutput:\n\n```\nPriority 1: 11 reassignments\nPriority 3: 6 reassignments\nPriority 5: 5 reassignments\n```\n\n## Error handling\n\nThe GlideQuery script include throws an error when your query has a problem and includes a clear explanation to help guide you. This script include checks for:\n\n-   Invalid fields\n-   Invalid value types for a field\n-   Invalid values for choice fields\n-   Invalid query operators\n\nFor example, the following code sample would throw an error because the queried field does not exist in the table.\n\n```\nnew global.GlideQuery('task')\n    .where('id', '4717dfe5a9fe198100450448b2404c16') // should be 'sys_id'\n    .select('description', 'severity')\n    .toArray(100);\n  // Error: Unable to find field 'id' in table 'task'. Known fields: active, activity_due, ...\n```\n\nThis code sample would throw an error because the data type of one of the arguments is incorrect.\n\n```\nnew global.GlideQuery('task')\n    .where('priority', 'one') // priority is an integer (should be 1)\n    .select('description', 'severity')\n    .toArray(100);\n  // Error: Unable to match value ['one'] with field 'priority' in table 'task'. Expecting type 'integer'\n```\n\n## Reuse\n\nBecause GlideQuery 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.\n\n```\nvar highPriorityTasks = new global.GlideQuery('task')\n    .where('active', true)\n    .where('priority', 1);\n\ngenerateReport(highPriorityTasks);\nnotifyOwners(highPriorityTasks);\nvar avgReassignmentCount = highPriorityTasks\n    .avg('reassignment_count')\n    .orElse(0)\n\n```\n\n## Limitations\n\nThe GlideQuery script include does not support:\n\n-   Reading or writing to tables that do not allow access from other scopes.\n-   Reading encoded queries.\n-   GlideDate or GlideDateTime objects, which are read as JavaScript strings.\n-   FX currency fields.\n-   Updating journal field types.\n-   Queries with ambiguous conditional logic. For example, the following query is unclear because the system does not know whether to execute `(active = true AND name != null) OR last_name = Luddy` or `active = true AND (name != null OR last_name = Luddy)`.\n\n    ```\n    var user = new global.GlideQuery('sys_user')\n      .where('active', true)\n      .whereNotNull('name')\n      .orWhere('last_name', 'Luddy')\n      .selectOne()\n      .get()\n    ```\n\n    See the [where\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/GlideQueryGlobalAPI.md) method to understand how to nest a child query instead.\n\n\n**Note:** Because the GlideQuery script include 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.\n\n## Intermediate and terminal methods in GlideQuery\n\nThe GlideQuery script include uses two categories of methods: intermediate and terminal. Intermediate methods are those methods that return a [Stream](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/StreamGlobalAPI.md), an API used for interacting with a stream of items like records, allowing a fluent style of syntax where calls are chained together. Terminal methods are those methods that do not return a stream and therefore stop the chain of Stream method calls.\n\nIn the GlideQuery script include, methods such as where\\(\\), orderBy\\(\\), and disableWorkflow\\(\\) are intermediate methods that return a new GlideQuery object. Similarly, the GlideQuery script include's most popular terminal methods, select\\(\\) and selectOne\\(\\), are terminal methods. These methods are called when GlideQuery is done being configured and is ready to start processing records. The differences between these two method types is important to keep in mind when configuring calls to the GlideQuery script include according to your specific use cases. For more information about intermediate and terminal methods, see the article [GlideQuery - Stream Processing Part 1](https://developer.servicenow.com/blog.do?p=/post/glidequery-p6/).\n\nThe following methods are classified as intermediate:\n\n-   aggregate\\(String aggregateType, String field\\)\n-   disableAutoSysFields\\(\\)\n-   disableWorkflow\\(\\)\n-   forceUpdate\\(\\)\n-   groupBy\\(String fields\\)\n-   having\\(String aggregateType, String field, String operator, Number value\\)\n-   limit\\(Number limit\\)\n-   orderBy\\(String fields\\)\n-   orderByDesc\\(String fieldOrAggregate, String field\\)\n-   orWhere\\(String fieldOrQuery, String operator, Any value\\)\n-   orWhereNotNull\\(String field\\)\n-   orWhereNull\\(String field\\)\n-   where\\(String fieldOrQuery, String operator, Any value\\)\n-   whereNotNull\\(String field\\)\n-   whereNull\\(String field\\)\n-   withAcls\\(\\)\n\nThe following methods are classified as terminal:\n\n-   avg\\(String field\\)\n-   count\\(\\)\n-   deleteMultiple\\(\\)\n-   get\\(String key, Array selectedFields\\)\n-   getBy\\(Object keyValues, Array selectedFields\\)\n-   insert\\(Object keyValues, Object selectedFields\\)\n-   insertOrUpdate\\(Object changes, Object selectedFields\\)\n-   max\\(String field\\)\n-   min\\(String field\\)\n-   select\\(String fields\\)\n-   selectOne\\(String fields\\)\n-   sum\\(String field\\)\n-   toGlideRecord\\(\\)\n-   update\\(Object changes, Object selectedFields\\)\n-   updateMultiple\\(Object changes\\)\n\n**Note:** parse\\(String table, String encoded\\_query\\) is neither intermediate or terminal, but static.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## GlideQuery - GlideQuery\\(String table\\)\n\nInstantiates a GlideQuery object used to build and execute record queries.\n\n|Name|Type|Description|\n|----|----|-----------|\n|table|String|Table to query.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThis example instantiates a query of the User table.\n\n```\nvar query = new global.GlideQuery('sys_user');\n```\n\n## GlideQuery - aggregate\\(String aggregateType, String field\\)\n\nAggregates a field using a specified aggregation function.\n\nUse this method to build queries that aggregate against multiple fields or use multiple aggregate functions, or if you must use the groupBy\\(\\) method. If you only want to aggregate against one field with one function, and you don't need to use groupBy\\(\\), then use one of these methods instead:\n\n-   avg\\(\\)\n-   min\\(\\)\n-   max\\(\\)\n-   count\\(\\)\n\n<table id=\"table_thy_rmc_nmb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\naggregateType\n\n</td><td>\n\nString\n\n</td><td>\n\nThe type of aggregation function to perform. Options include:-   `min`: Returns the smallest value of all matching records.\n-   `max`: Returns the largest value of all matching records.\n-   `sum`: Returns the sum of all matching records.\n-   `avg`: Returns the average of all matching records.\n-   `count`: Returns the number of number of matching records.\n\n</td></tr><tr><td>\n\nfield\n\n</td><td>\n\nString\n\n</td><td>\n\nField on which to perform the operation.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis script performs a query on the Task table, groups the records by priority, and returns each priority that has total reassignments greater than four.\n\n```\nvar query = new global.GlideQuery('task')\n    .where('active', true) //Returns new GlideQuery object with a \"where\" clause.\n    .groupBy('priority') //Returns new GlideQuery object with a \"group by\" clause.\n    .aggregate('sum', 'reassignment_count') //Returns new GlideQuery object with a \"sum(reassignment_count)\" clause.\n    .having('sum', 'reassignment_count', '>', 4) //Returns new GlideQuery object with a \"having reassignment_count > 4\" clause.\n    .select() //Returns a stream of records wrapped in a Stream object.  \n    .forEach(function (priority){ //Terminal method in the Stream class that executes the query and returns the result. \n      gs.info(\"Priority \" + priority.group.priority + \": \" + priority.sum.reassignment_count + \" reassignments\");\n    });\n```\n\nOutput:\n\n```\nPriority 1: 11 reassignments\nPriority 3: 6 reassignments\nPriority 5: 5 reassignments\n```\n\n## GlideQuery - avg\\(String field\\)\n\nReturns the aggregate average of a given numeric field.\n\nYou can only use this method on fields of the following types:\n\n-   Integer\n-   Long\n-   Floating point number\n-   Double\n-   Currency\n\n|Name|Type|Description|\n|----|----|-----------|\n|field|String|Field on which to perform the operation.|\n\n|Type|Description|\n|----|-----------|\n|Optional|Object that contains the aggregate average of the given field.|\n\nThis example shows how to return the average number of faults in the cmdb\\_ci table.\n\n```\nvar faults = new global.GlideQuery('cmdb_ci')\n    .avg('fault_count')\n    .orElse(0);\n\ngs.info(JSON.stringify(faults));\n```\n\nOutput:\n\n```\n0.0037\n```\n\n## GlideQuery - count\\(\\)\n\nReturns the number of records that match the query.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Number|Number of records that match the query.|\n\nThis example returns the number of active records in the User table.\n\n```\nvar userCount = new global.GlideQuery('sys_user')\n    .where('active', true)\n    .count();\n```\n\nOutput:\n\n```\n612\n```\n\n## GlideQuery - deleteMultiple\\(\\)\n\nDeletes all records in the table specified by the preceding Where clauses.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThis example deletes all active records in the User table where the last name is Jeter.\n\n```\nvar query = new global.GlideQuery('sys_user')\n    .where('active', true)\n    .where('last_name', 'Jeter')\n    .deleteMultiple();\n```\n\n## GlideQuery - disableAutoSysFields\\(\\)\n\nDisables updating system fields, or fields with a name that starts with the `sys` prefix, such as sys\\_created\\_on, sys\\_updated\\_on, and sys\\_mod\\_count. Only applies to the specified query.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis 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.\n\n```\nvar query = new global.GlideQuery('task')\n    .disableAutoSysFields()\n    .insert({ description: 'example', priority: 1 });\n```\n\n## GlideQuery - disableWorkflow\\(\\)\n\nDisables any business rules, flows, workflows, or audit records that would run or be created as the result of the query.\n\n**Note:** This method only works for tables in the GLOBAL scope.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis example updates multiple records in the Task table without triggering any automatic business processes.\n\n```\nvar query = new global.GlideQuery('task')\n    .disableWorkflow()\n    .where('active', true)\n    .updateMultiple({ priority: 1 });\n```\n\n## GlideQuery - forceUpdate\\(\\)\n\nForces a database update even when no record changes are made. For example, you can use this method to force a business rule to execute.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis example force updates Task records with a certain sys\\_id.\n\n```\nvar forceUpdate = new global.GlideQuery('task')\n    .forceUpdate()\n    .where('sys_id', taskId)\n    .update()\n```\n\n## GlideQuery - get\\(String key, Array selectedFields\\)\n\nReturns a single record from the query.\n\n<table id=\"table_u1k_flc_nmb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nkey\n\n</td><td>\n\nString\n\n</td><td>\n\nSys\\_id of the record to return.\n\n</td></tr><tr><td>\n\nselectedFields\n\n</td><td>\n\nArray\n\n</td><td>\n\nOptional. Additional fields to return in the result. Default: The system always returns the sys\\_id.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|Optional|Object used to interact with a single record.|\n\nExample that returns a record based on sys\\_id.\n\n```\nvar user = new global.GlideQuery('sys_user')\n    .get('5137153cc611227c000bbd1bd8cd2005', ['first_name', 'last_name']) //Returns an Optional object.\n    .orElse({ first_name: 'Default', last_name: 'User' }); //Method in the Optional class to return a default value.\n\ngs.info(JSON.stringify(user, null, 2));\n```\n\nOutput:\n\n```\n{\n   \"sys_id\":\"5137153cc611227c000bbd1bd8cd2005\",\n   \"first_name\":\"Fred\",\n   \"last_name\":\"Luddy\"\n}\n```\n\n## GlideQuery - getBy\\(Object keyValues, Array selectedFields\\)\n\nReturns 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.\n\n<table id=\"table_ift_dlc_nmb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nkeyValues\n\n</td><td>\n\nObject\n\n</td><td>\n\nObject where the keys are the name of the fields, and the values are the values to query for.\n\n</td></tr><tr><td>\n\nselectedFields\n\n</td><td>\n\nArray\n\n</td><td>\n\nOptional. Additional fields to return in the result. Default: The system always returns the sys\\_id.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|Optional|Object used to interact with a single record.|\n\nExample that returns a record by querying for a user's name.\n\n```\nvar user = new global.GlideQuery('sys_user')\n    .getBy({\n        first_name: 'Fred',\n        last_name: 'Luddy'\n    }, ['first_name', 'last_name', 'city', 'active']) // select first_name, last_name, city, active\n    .orElse({\n        first_name: 'Nobody',\n        last_name: 'Found',\n        city: 'Nowhere',\n        active: false\n    });\n\ngs.info(JSON.stringify(user, null, 2));\n```\n\nOutput:\n\n```\n{\n   \"first_name\":\"Fred\",\n   \"last_name\":\"Luddy\",\n   \"city\":null,\n   \"active\":true,\n   \"sys_id\":\"5137153cc611227c000bbd1bd8cd2005\"\n}\n```\n\n## GlideQuery - groupBy\\(String fields\\)\n\nGroups the query results by a designated field or fields.\n\nYou must use this method with the aggregate\\(\\) method.\n\n|Name|Type|Description|\n|----|----|-----------|\n|fields|String or Array of Strings|Field or fields to group the results by.|\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis script performs a query on the Task table, groups the records by priority, and returns each priority that has total reassignments greater than four.\n\n```\nvar query = new global.GlideQuery('task')\n    .where('active', true) //Returns new GlideQuery object with a \"where\" clause.\n    .groupBy('priority') //Returns new GlideQuery object with a \"group by\" clause.\n    .aggregate('sum', 'reassignment_count') //Returns new GlideQuery object with a \"sum(reassignment_count)\" clause.\n    .having('sum', 'reassignment_count', '>', 4) //Returns new GlideQuery object with a \"having reassignment_count > 4\" clause.\n    .select() //Returns a stream of records wrapped in a Stream object.  \n    .forEach(function (priority){ //Terminal method in the Stream class that executes the query and returns the result. \n      gs.info(\"Priority \" + priority.group.priority + \": \" + priority.sum.reassignment_count + \" reassignments\");\n    });\n```\n\nOutput:\n\n```\nPriority 1: 11 reassignments\nPriority 3: 6 reassignments\nPriority 5: 5 reassignments\n```\n\n## GlideQuery - having\\(String aggregateType, String field, String operator, Number value\\)\n\nFilters aggregate groups so that you can display only groups of results that match a specified condition.\n\nMust use this method with the aggregate\\(\\) or groupBy\\(\\) methods.\n\n<table id=\"table_jw3_vmc_nmb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\naggregateType\n\n</td><td>\n\nString\n\n</td><td>\n\nThe type of aggregation function to perform. Options include:-   `min`: Returns the smallest value of all matching records.\n-   `max`: Returns the largest value of all matching records.\n-   `sum`: Returns the sum of all matching records.\n-   `avg`: Returns the average of all matching records.\n-   `count`: Returns the number of number of matching records.\n\n</td></tr><tr><td>\n\nfield\n\n</td><td>\n\nString\n\n</td><td>\n\nField on which to perform the operation.\n\n</td></tr><tr><td>\n\noperator\n\n</td><td>\n\nString\n\n</td><td>\n\nNumeric operator to use in the operation. Options include:\n\n-   `>`: Greater than.\n-   `<`: Less than.\n-   `>=`: Greater than or equal to.\n-   `<=`: Less than or equal to.\n-   `=`: Equal to.\n-   `!=`: Not equal to.\n\n</td></tr><tr><td>\n\nvalue\n\n</td><td>\n\nNumber\n\n</td><td>\n\nNumber value to use in the operation.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis script performs a query on the Task table, groups the records by priority, and returns each priority that has total reassignments greater than four.\n\n```\nvar query = new global.GlideQuery('task')\n    .where('active', true) //Returns new GlideQuery object with a \"where\" clause.\n    .groupBy('priority') //Returns new GlideQuery object with a \"group by\" clause.\n    .aggregate('sum', 'reassignment_count') //Returns new GlideQuery object with a \"sum(reassignment_count)\" clause.\n    .having('sum', 'reassignment_count', '>', 4) //Returns new GlideQuery object with a \"having reassignment_count > 4\" clause.\n    .select() //Returns a stream of records wrapped in a Stream object.  \n    .forEach(function (priority){ //Terminal method in the Stream class that executes the query and returns the result. \n      gs.info(\"Priority \" + priority.group.priority + \": \" + priority.sum.reassignment_count + \" reassignments\");\n    });\n```\n\nOutput:\n\n```\nPriority 1: 11 reassignments\nPriority 3: 6 reassignments\nPriority 5: 5 reassignments\n```\n\n## GlideQuery - insert\\(Object keyValues, Object selectedFields\\)\n\nInserts a record and returns an Optional object containing the record.\n\n<table id=\"table_m5f_hlc_nmb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nkeyValues\n\n</td><td>\n\nObject\n\n</td><td>\n\nObject containing name-value pairs to insert into the record. Unspecified fields will be null.\n\n</td></tr><tr><td>\n\nselectedFields\n\n</td><td>\n\nArray\n\n</td><td>\n\nOptional. Additional fields to return in the result. Default: The system always returns the sys\\_id.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|Optional|Object used to interact with a single record.|\n\nThis example shows how to insert a record based on a user's first and last name.\n\n```\nvar fred = new global.GlideQuery('sys_user')\n    .insert({ first_name: 'Fred', last_name: 'Luddy' })\n    .get();\n\ngs.info(JSON.stringify(fred, null, 2));\n```\n\nOutput:\n\n```\n{\n   \"sys_id\":\"cf16eed0e82a9010f8778bda83d255d2\",\n   \"first_name\":\"Fred\",\n   \"last_name\":\"Luddy\"\n}\n```\n\n## GlideQuery - insertOrUpdate\\(Object changes, Object selectedFields\\)\n\nUpdates an existing record, or inserts a new record if one does not already exist.\n\n<table id=\"table_sdt_3lc_nmb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nchanges\n\n</td><td>\n\nObject\n\n</td><td>\n\nObject containing name-value pairs to update or insert into the record.\n\n</td></tr><tr><td>\n\nselectedFields\n\n</td><td>\n\nArray\n\n</td><td>\n\nOptional. Additional fields to return in the result. Default: The system always returns the sys\\_id.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|Optional|Object used to interact with a single record.|\n\nThis example shows how to insert a new record that does not already exist in the system.\n\n```\n// insert a new record\nvar user = new GlideQuery('sys_user')\n    .insertOrUpdate({\n        first_name: 'George',\n        last_name: 'Griffey'\n    })\n    .orElse(null);\n```\n\nThis example shows how to update an existing record.\n\n```\n// update existing record\nvar user = new global.GlideQuery('sys_user')\n    .insertOrUpdate({\n        sys_id: '2d0efd6c73662300bb513198caf6a72e',\n        first_name: 'George',\n        last_name: 'Griffey' })\n    .orElse(null);\n```\n\n## GlideQuery - limit\\(Number limit\\)\n\nLimits the number of records returned in a query.\n\n|Name|Type|Description|\n|----|----|-----------|\n|limit|Number|Number of records to return.|\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis example shows how to limit the results returned to five records.\n\n```\nvar incidents = new global.GlideQuery('incident')\n    .limit(5)\n    .select('priority', 'description')\n    .forEach(function (incident){\n        gs.info(JSON.stringify(incident, null, 2));\n    });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"priority\": 1,\n  \"description\": \"User can't get to any of his files on the file server.\",\n  \"sys_id\": \"9d385017c611228701d22104cc95c371\"\n}\n*** Script: {\n  \"priority\": 1,\n  \"description\": \"I just moved from floor 2 to floor 3 and my laptop cannot connect to any wireless network.\",\n  \"sys_id\": \"e8caedcbc0a80164017df472f39eaed1\"\n}\n*** Script: {\n  \"priority\": 1,\n  \"description\": \"User forgot their email password.\",\n  \"sys_id\": \"9d3c1197c611228701cd1d94bc32d76d\"\n}\n*** Script: {\n  \"priority\": 1,\n  \"description\": \"When I try to print, my whole computer just freezes and stops working.\",\n  \"sys_id\": \"8d6246c7c0a80164012fb063cecd4ace\"\n}\n*** Script: {\n  \"priority\": 3,\n  \"description\": \"Unable to login even though login credentials are correct.\",\n  \"sys_id\": \"a9e30c7dc61122760116894de7bcc7bd\"\n}\n```\n\n## GlideQuery - max\\(String field\\)\n\nReturns the aggregate maximum of a given field.\n\n|Name|Type|Description|\n|----|----|-----------|\n|field|String|Field on which to perform the operation.|\n\n|Type|Description|\n|----|-----------|\n|Optional|Object used to interact with a single record.|\n\nThis example shows how to return the maximum value, or highest alphanumeric value, of a given field.\n\n```\nvar name = new global.GlideQuery('sys_user')\n    .max('last_name')\n    .orElse('');\n\ngs.info(JSON.stringify(name));\n```\n\nOutput:\n\n```\n\"Zortman\"\n```\n\n## GlideQuery - min\\(String field\\)\n\nReturns the aggregate minimum of a given field.\n\n|Name|Type|Description|\n|----|----|-----------|\n|field|String|Field on which to perform the operation.|\n\n|Type|Description|\n|----|-----------|\n|Optional|Object used to interact with a single record.|\n\nThis example shows how to return the minimum value, or lowest alphanumeric value, of a given field.\n\n```\nvar name = new global.GlideQuery('sys_user')\n    .min('last_name')\n    .orElse('');\n\ngs.info(JSON.stringify(name));\n```\n\nOutput:\n\n```\n\"Abel\"\n```\n\n## GlideQuery - orderBy\\(String fields\\)\n\nOrders the returned result in ascending order by a given field.\n\n|Name|Type|Description|\n|----|----|-----------|\n|fields|String|Comma-delimited fields to order the result by in ascending order.|\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis example shows how to order results in ascending order by record number.\n\n```\nvar query = new global.GlideQuery('incident')\n    .orderBy('number')\n    .limit(5)\n    .select('number', 'description') //Returns a stream of records wrapped in a Stream object.  \n    .forEach(function (incident){ //Terminal method in the Stream class that executes the query and returns the result.\n        gs.info(JSON.stringify(incident, null, 2));\n    });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"number\": \"INC0000001\",\n  \"description\": \"User can't access email on mail.company.com.\",\n  \"sys_id\": \"9c573169c611228700193229fff72400\"\n}\n*** Script: {\n  \"number\": \"INC0000002\",\n  \"description\": \"User can't get to any of his files on the file server.\",\n  \"sys_id\": \"9d385017c611228701d22104cc95c371\"\n}\n*** Script: {\n  \"number\": \"INC0000003\",\n  \"description\": \"I just moved from floor 2 to floor 3 and my laptop cannot connect to any wireless network.\",\n  \"sys_id\": \"e8caedcbc0a80164017df472f39eaed1\"\n}\n*** Script: {\n  \"number\": \"INC0000004\",\n  \"description\": \"User forgot their email password.\",\n  \"sys_id\": \"9d3c1197c611228701cd1d94bc32d76d\"\n}\n*** Script: {\n  \"number\": \"INC0000005\",\n  \"description\": \"CPU was 100% busy for more than 10 minutes\",\n  \"sys_id\": \"e8e875b0c0a80164009dc852b4d677d5\"\n}\n```\n\n## GlideQuery - orderByDesc\\(String fieldOrAggregate, String field\\)\n\nOrders the returned result in descending order by a given field.\n\n<table id=\"table_yxt_cmc_nmb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nfieldOrAggregate\n\n</td><td>\n\nString\n\n</td><td>\n\nIf the query does not use the aggregate\\(\\) method, pass the field to order the results by. If the query uses the aggregate\\(\\) method, pass the type of aggregation function to perform.\n\n Options include:\n\n-   `min`: Returns the smallest value of all matching records.\n-   `max`: Returns the largest value of all matching records.\n-   `sum`: Returns the sum of all matching records.\n-   `avg`: Returns the average of all matching records.\n-   `count`: Returns the number of number of matching records.\n\n</td></tr><tr><td>\n\nfield\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. Field to order the result by in descending order. Required for queries using the aggregate\\(\\) method.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis example shows how to order the result in descending order by number.\n\n```\nvar query = new global.GlideQuery('incident')\n    .orderByDesc('number')\n    .limit(5)\n    .select('number', 'description') //Returns a stream of records wrapped in a Stream object.  \n    .forEach(function (incident){ //Terminal method in the Stream class that executes the query and returns the result.\n        gs.info(JSON.stringify(incident, null, 2));\n    });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"number\": \"INC0009009\",\n  \"description\": \"Unable to access the shared folder. Please provide access.\",\n  \"sys_id\": \"57af7aec73d423002728660c4cf6a71c\"\n}\n*** Script: {\n  \"number\": \"INC0009005\",\n  \"description\": \"Unable to send or receive emails.\",\n  \"sys_id\": \"ed92e8d173d023002728660c4cf6a7bc\"\n}\n*** Script: {\n  \"number\": \"INC0009004\",\n  \"description\": \"While launching the defect tracking base URL, it is redirecting to an error page.\",\n  \"sys_id\": \"e329de99731423002728660c4cf6a73c\"\n}\n*** Script: {\n  \"number\": \"INC0009003\",\n  \"description\": \"Having an issue with users trying to access the company portal app\",\n  \"sys_id\": \"9fffc328731823002728660c4cf6a742\"\n}\n*** Script: {\n  \"number\": \"INC0009002\",\n  \"description\": \"My computer is not detecting the headphone device. It could be an issue with the USB port.\",\n  \"sys_id\": \"1c832706732023002728660c4cf6a7b9\"\n}\n```\n\nThis example shows how to order an aggregate result by the sum of child incidents.\n\n```\nvar aggQuery = new GlideQuery('incident')\n    .aggregate('sum', 'child_incidents')\n    .groupBy('category')\n    .orderByDesc('sum', 'child_incidents')\n    .select()\n    .forEach(function (category){\n        gs.info(JSON.stringify(category, null, 2));    \n    });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"group\": {\n    \"category\": \"hardware\"\n  },\n  \"sum\": {\n    \"child_incidents\": 2\n  }\n}\n*** Script: {\n  \"group\": {\n    \"category\": \"inquiry\"\n  },\n  \"sum\": {\n    \"child_incidents\": 1\n  }\n}\n*** Script: {\n  \"group\": {\n    \"category\": \"software\"\n  },\n  \"sum\": {\n    \"child_incidents\": 0\n  }\n}\n*** Script: {\n  \"group\": {\n    \"category\": \"\"\n  },\n  \"sum\": {\n    \"child_incidents\": null\n  }\n}\n*** Script: {\n  \"group\": {\n    \"category\": \"database\"\n  },\n  \"sum\": {\n    \"child_incidents\": null\n  }\n}\n*** Script: {\n  \"group\": {\n    \"category\": \"network\"\n  },\n  \"sum\": {\n    \"child_incidents\": null\n  }\n}\n```\n\n## GlideQuery - orWhere\\(String fieldOrQuery, String operator, Any value\\)\n\nAdds an OR clause to a query that returns values based on a given condition.\n\n**Note:** Precede this method with the where\\(\\), whereNull\\(\\), or whereNotNull\\(\\) methods.\n\n|Name|Type|Description|\n|----|----|-----------|\n|fieldOrQuery|String or [GlideQuery](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/GlideQueryGlobalAPI.md)|Field or another GlideQuery object used in the where clause. If passing a field, you can dot-walk to a desired value. For example, `'company.name'`.|\n|operator|String|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.|\n|value|Any|Value used in the OR clause.|\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis example shows how to add a simple OR clause to a query.\n\n```\nvar query = new global.GlideQuery('sys_user')\n   .where('failed_attempts', '>', 0)\n   .orWhere('last_login', '<', '2019-04-15')\n   .select()\n   .forEach(function (user){\n      gs.info(JSON.stringify(user, null, 2));\n   });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"sys_id\": \"005d500b536073005e0addeeff7b12f4\"\n}\n*** Script: {\n  \"sys_id\": \"d999e5fc77e72300454792718a10611d\"\n}\n*** Script: {\n  \"sys_id\": \"30ad318577ab2300454792718a10619e\"\n}\n*** Script: {\n  \"sys_id\": \"4ac73ecd738123002728660c4cf6a72c\"\n}\n*** Script: {\n  \"sys_id\": \"3883f4c0730123002728660c4cf6a754\"\n}\n*** Script: {\n  \"sys_id\": \"3988a3ca732023002728660c4cf6a757\"\n}\n*** Script: {\n  \"sys_id\": \"8ff5b254b33213005e3de13516a8dcf7\"\n}\n```\n\nThis example shows how to add an `orWhere` clause that contains a separate query.\n\n```\n// active = true OR (title = 'Vice President' AND state = 'CA')\nvar query = new GlideQuery('sys_user')\n   .where('active', true)\n   .orWhere(new GlideQuery()\n       .where('title', 'Vice President')\n       .where('state', 'CA'))\n   .select('name')\n   .limit(5)\n   .forEach(function (user){\n      gs.info(JSON.stringify(user, null, 2));\n   });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"name\": \"ITIL User\",\n  \"sys_id\": \"681b365ec0a80164000fb0b05854a0cd\"\n}\n*** Script: {\n  \"name\": \"SOAP Guest\",\n  \"sys_id\": \"155699460a0a0b2b009e27c10b7f68f6\"\n}\n*** Script: {\n  \"name\": \"System Administrator\",\n  \"sys_id\": \"6816f79cc0a8016401c5a33be04be441\"\n}\n*** Script: {\n  \"name\": \"Alva Pennigton\",\n  \"sys_id\": \"ca826bf03710200044e0bfc8bcbe5d89\"\n}\n*** Script: {\n  \"name\": \"Benchmark Scheduler\",\n  \"sys_id\": \"d3383a875b0132003d1c484c11f91a5b\"\n}\n```\n\n## GlideQuery - orWhereNotNull\\(String field\\)\n\nAdds an OR clause that returns records that do not contain a null value in a given field.\n\n**Note:** Precede this method with the where\\(\\), whereNull\\(\\), or whereNotNull\\(\\) methods.\n\n|Name|Type|Description|\n|----|----|-----------|\n|field|String|Field used in the query.|\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis example shows how to query the User table and return results where the first or last names are not null.\n\n```\nvar query = new global.GlideQuery('sys_user')\n   .whereNotNull('first_name')\n   .orWhereNotNull('last_name')\n   .select('name')\n   .limit(5)\n   .forEach(function (user){\n      gs.info(JSON.stringify(user, null, 2));\n   });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"name\": \"ITIL User\",\n  \"sys_id\": \"681b365ec0a80164000fb0b05854a0cd\"\n}\n*** Script: {\n  \"name\": \"System Administrator\",\n  \"sys_id\": \"6816f79cc0a8016401c5a33be04be441\"\n}\n*** Script: {\n  \"name\": \"Alva Pennigton\",\n  \"sys_id\": \"ca826bf03710200044e0bfc8bcbe5d89\"\n}\n*** Script: {\n  \"name\": \"Benchmark Scheduler\",\n  \"sys_id\": \"d3383a875b0132003d1c484c11f91a5b\"\n}\n*** Script: {\n  \"name\": \"Allyson Gillispie\",\n  \"sys_id\": \"06826bf03710200044e0bfc8bcbe5d8a\"\n}\n```\n\n## GlideQuery - orWhereNull\\(String field\\)\n\nAdds an OR clause to a query that returns records that contain a null value in a given field.\n\n**Note:** Precede this method with the where\\(\\), whereNull\\(\\), or whereNotNull\\(\\) methods.\n\n|Name|Type|Description|\n|----|----|-----------|\n|field|String|Field used in the query.|\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis example shows how to query the User table and return records where the first or last names are null.\n\n```\nvar query = new global.GlideQuery('sys_user')\n   .whereNull('last_name')\n   .orWhereNull('first_name')\n   .select('first_name', 'last_name')\n   .forEach(function (user){\n      gs.info(JSON.stringify(user, null, 2));\n   });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"first_name\": \"ml.admin\",\n  \"last_name\": null,\n  \"sys_id\": \"88aad6c5c73003005f1b78d48b9763a5\"\n}\n*** Script: {\n  \"first_name\": \"Sitemap Scheduler User\",\n  \"last_name\": null,\n  \"sys_id\": \"85388c25b71011104eed4643ae11a993\"\n}\n*** Script: {\n  \"first_name\": null,\n  \"last_name\": \"Guest\",\n  \"sys_id\": \"5136503cc611227c0183e96598c4f706\"\n}\n*** Script: {\n  \"first_name\": \"ml_report.user\",\n  \"last_name\": null,\n  \"sys_id\": \"b6bad6c5c73003005f1b78d48b97632a\"\n}\n```\n\n## GlideQuery - parse\\(String table, String encoded\\_query \\)\n\nAdds an encoded query to a new GlideQuery query.\n\nThis method does not support all GlideRecord encoded query operators. The following operators are currently supported:\n\n<table><tbody><tr><td>\n\n`=`\n\n</td><td>\n\n`ANYTHING`\n\n</td><td>\n\n`GT_FIELD`\n\n</td><td>\n\n`NOT IN`\n\n</td></tr><tr><td>\n\n`!=`\n\n</td><td>\n\n`BETWEEN`\n\n</td><td>\n\n`GT_OR_EQUALS_FIELD`\n\n</td><td>\n\n`NOT LIKE`\n\n</td></tr><tr><td>\n\n`>`\n\n</td><td>\n\n`CONTAINS`\n\n</td><td>\n\n`IN`\n\n</td><td>\n\n`NSAMEAS`\n\n</td></tr><tr><td>\n\n`>=`\n\n</td><td>\n\n`DOES NOT CONTAIN`\n\n</td><td>\n\n`INSTANCEOF`\n\n</td><td>\n\n`ON`\n\n</td></tr><tr><td>\n\n`<`\n\n</td><td>\n\n`DYNAMIC`\n\n</td><td>\n\n`LIKE`\n\n</td><td>\n\n`SAMEAS`\n\n</td></tr><tr><td>\n\n`<=`\n\n</td><td>\n\n`EMPTYSTRING`\n\n</td><td>\n\n`LT_FIELD`\n\n</td><td>\n\n`STARTSWITH`\n\n</td></tr><tr><td>\n\n \n\n</td><td>\n\n`ENDSWITH`\n\n</td><td>\n\n`LT_OR_EQUALS_FIELD`\n\n</td><td>\n\n \n\n</td></tr></tbody>\n</table>|Name|Type|Description|\n|----|----|-----------|\n|table|String|Table to query, such as task or incident.|\n|encoded\\_query|String|[Encoded query](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-user-interface/c_EncodedQueryStrings.md) to apply to the records in the specified table.|\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|GlideQuery object containing the encoded query.|\n\nThe 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.\n\n```\nGlideQuery.parse('task', 'active=true^ORDERBYpriority') // Pass the encoded query to use to parse the Task records\n  .select('description') // Return the records that match the encoded query\n  .forEach(doSomething); // Do some processing on each of the returned records\n```\n\n## GlideQuery - select\\(String fields\\)\n\nReturns the results of the query as a Stream object containing the specified fields.\n\n**Note:** Use a terminal method in the Stream class to get the result of the query. For more information, see [Stream](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/StreamGlobalAPI.md).\n\nYou 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 `company$DISPLAY` returns the display value of a company field. Possible flags include:\n\n-   `DISPLAY`: Returns the display value of a field.\n-   `CURRENCY_CODE`: Returns the currency code of a currency field. For example, `USD`.\n-   `CURRENCY_DISPLAY`: Returns the currency display value of a currency field. For example, `¥123.45`.\n-   `CURRENCY_STRING`: Returns the currency string of a currency field. For example, `JPY;123.45`.\n\n<table id=\"table_ktr_qlc_nmb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nfields\n\n</td><td>\n\nString or Array of Strings\n\n</td><td>\n\nOptional. 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: ```\nselect('first_name', 'location.city', 'company$DISPLAY');\n```\n\nor```\nselect(['first_name', 'location.city', 'company$DISPLAY']);\n```\n\n Default: The system always returns the sys\\_id.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|Stream|Object used to interact with a stream of items such as records.|\n\nThis example shows how to select fields to display from the query and use `$DISPLAY` to return the display value of a field.\n\n```\nvar stream = new global.GlideQuery('sys_user')\n   .select('first_name', 'last_name', 'company$DISPLAY')\n   .limit(5)\n   .forEach(function (user){\n      gs.info(JSON.stringify(user, null, 2));\n   });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"first_name\": \"System\",\n  \"last_name\": \"Administrator\",\n  \"company$DISPLAY\": \"\",\n  \"sys_id\": \"6816f79cc0a8016401c5a33be04be441\"\n}\n*** Script: {\n  \"first_name\": \"Alva\",\n  \"last_name\": \"Pennigton\",\n  \"company$DISPLAY\": \"ACME North America\",\n  \"sys_id\": \"ca826bf03710200044e0bfc8bcbe5d89\"\n}\n*** Script: {\n  \"first_name\": \"Benchmark\",\n  \"last_name\": \"Scheduler\",\n  \"company$DISPLAY\": \"\",\n  \"sys_id\": \"d3383a875b0132003d1c484c11f91a5b\"\n}\n*** Script: {\n  \"first_name\": \"Allyson\",\n  \"last_name\": \"Gillispie\",\n  \"company$DISPLAY\": \"ACME North America\",\n  \"sys_id\": \"06826bf03710200044e0bfc8bcbe5d8a\"\n}\n*** Script: {\n  \"first_name\": \"SOAP\",\n  \"last_name\": \"Guest\",\n  \"company$DISPLAY\": \"\",\n  \"sys_id\": \"155699460a0a0b2b009e27c10b7f68f6\"\n}\n```\n\n## GlideQuery - selectOne\\(String fields\\)\n\nReturns the result of the query as an Optional object containing specified fields.\n\nUse this method when returning a single record, or to test if a record exists. If returning multiple records, use the select\\(\\) method to return a Stream object.\n\nYou 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 `company$DISPLAY` returns the display value of a company field. Possible flags include:\n\n-   `DISPLAY`: Returns the display value of a field.\n-   `CURRENCY_CODE`: Returns the currency code of a currency field. For example, `USD`.\n-   `CURRENCY_DISPLAY`: Returns the currency display value of a currency field. For example, `¥123.45`.\n-   `CURRENCY_STRING`: Returns the currency string of a currency field. For example, `JPY;123.45`.\n\n<table id=\"table_pw3_slc_nmb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nfields\n\n</td><td>\n\nString or Array of Strings\n\n</td><td>\n\nOptional. 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: ```\nselectOne('first_name', 'location.city', 'company$DISPLAY');\n```\n\nor```\nselectOne(['first_name', 'location.city', 'company$DISPLAY']);\n```\n\n Default: The system always returns the sys\\_id.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|Optional|Object used to interact with a single record.|\n\nThis example shows how to return a single record as an Optional object and display specified fields.\n\n```\nvar user = new global.GlideQuery('sys_user')\n    .where('zip', '12345')\n    .whereNotNull('last_name')\n    .selectOne('first_name', 'last_name', 'company$DISPLAY')\n    .get();\n\ngs.info(JSON.stringify(user, null, 2));\n```\n\nOutput:\n\n```\n{\n   \"first_name\":\"Abel\",\n   \"last_name\":\"Tuter\",\n   \"company$DISPLAY\":\"ACME South America\",\n   \"sys_id\":\"62826bf03710200044e0bfc8bcbe5df1\"\n}\n```\n\n## GlideQuery - sum\\(String field\\)\n\nReturns the aggregate sum of a given numeric field.\n\nYou can only use this method on fields of the following types:\n\n-   Integer\n-   Long\n-   Floating point number\n-   Double\n-   Currency\n\n|Name|Type|Description|\n|----|----|-----------|\n|field|String|Field on which to perform the operation.|\n\n|Type|Description|\n|----|-----------|\n|Optional|Object used to interact with a single record.|\n\nThis example shows how to return the sum of all faults in the cmdb\\_ci table.\n\n```\nvar totalFaults = new global.GlideQuery('cmdb_ci')\n    .sum('fault_count')\n    .orElse(0);\n\ngs.info(JSON.stringify(totalFaults));\n```\n\nOutput:\n\n```\n10\n```\n\n## GlideQuery - toGlideRecord\\(\\)\n\nReturns a GlideRecord object that represents the current query. Returns a GlideAggregrate object if the query uses the GlideQuery.aggregate\\(\\) method.\n\nAfter transforming the query, use the query\\(\\) method in the [GlideRecord](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideRecordScopedAPI.md) or [GlideAggregate](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideAggregateScopedAPI.md) classes to query the database.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|GlideRecord or GlideAggregate|GlideRecord object that contains the query. If you used the GlideQuery.aggregate\\(\\) method, then the method returns a GlideAggregrate object instead.|\n\nThis example shows how to transform a GlideQuery object into a GlideRecord.\n\n```\nvar userGr = new global.GlideQuery('sys_user')\n    .where('active', true)\n    .whereNotNull('first_name')\n    .limit(10)\n    .toGlideRecord();\nuserGr.query();\n```\n\n## GlideQuery - update\\(Object changes, Object selectedFields\\)\n\nUpdates an existing record that matches the defined conditions.\n\nBefore using this method, call the where\\(\\) method to specify the conditions that a record must meet to be updated.\n\n<table id=\"table_cbg_klc_nmb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nchanges\n\n</td><td>\n\nObject\n\n</td><td>\n\nObject containing name-value pairs to update in the record. Names must match fields in the table.\n\n</td></tr><tr><td>\n\nselectedFields\n\n</td><td>\n\nArray\n\n</td><td>\n\nOptional. Additional fields to return in the result. Default: The system always returns the sys\\_id.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|Optional|Object used to interact with a single record.|\n\nThis example shows how to update a record with new values.\n\n```\nvar updateRecord = new global.GlideQuery('sys_user')\n    .where('sys_id', '0a826bf03710200044e0bfc8bcbe5d7a')\n    .update({ city: 'Los Angeles' });\n```\n\n## GlideQuery - updateMultiple\\(Object changes\\)\n\nUpdates all existing records that match the defined conditions. Returns the number of records updated.\n\nBefore using this method, call the where\\(\\) method to specify the conditions that the records must meet to be updated.\n\n|Name|Type|Description|\n|----|----|-----------|\n|changes|Object|Object containing name-value pairs to update in the record. Names must match fields in the table.|\n\n<table id=\"table_zwr_llc_nmb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nObject\n\n</td><td>\n\nObject containing the number of records that were updated. Keys include: -   `rowCount`: Number of rows updated in the table.\n\n</td></tr></tbody>\n</table>This example shows how to update any records that fit a defined criteria.\n\n```\nvar update = new global.GlideQuery('sys_user')\n    .where('active', false)\n    .where('last_name', 'Griffey')\n    .updateMultiple({ active: true });\n\ngs.info(JSON.stringify(update));\n```\n\nOutput:\n\n```\n{\"rowCount\":1}\n```\n\n## GlideQuery - where\\(String fieldOrQuery, String operator, Any value\\)\n\nAdds a Where clause to the query that returns values based on a given condition.\n\n**Note:** Do not precede this method with the orWhere\\(\\), orWhereNull\\(\\), or orWhereNotNull\\(\\) methods.\n\n|Name|Type|Description|\n|----|----|-----------|\n|fieldOrQuery|String or [GlideQuery](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/GlideQueryGlobalAPI.md)|Field or another GlideQuery object used in the where clause. If passing a field, you can dot-walk to a desired value. For example, `'company.name'`.|\n|operator|String|Optional. Operator used in the where clause. If you do not pass an argument, the system uses the = operator.|\n|value|Any|Value used in the where clause.|\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis example shows how to return records from the User table where active is true and the user's last login is after a specified date.\n\n```\nvar query = new global.GlideQuery('sys_user')\n   .where('active', true)\n   .where('last_login', '>', '2018-01-01')\n   .limit(5)\n   .select()\n   .forEach(function (user){\n      gs.info(JSON.stringify(user, null, 2));\n   });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"sys_id\": \"6816f79cc0a8016401c5a33be04be441\"\n}\n*** Script: {\n  \"sys_id\": \"005d500b536073005e0addeeff7b12f4\"\n}\n*** Script: {\n  \"sys_id\": \"d999e5fc77e72300454792718a10611d\"\n}\n*** Script: {\n  \"sys_id\": \"30ad318577ab2300454792718a10619e\"\n}\n*** Script: {\n  \"sys_id\": \"3883f4c0730123002728660c4cf6a754\"\n}\n```\n\nThis example shows how to return records from the Incident table where active is true and the priority or severity is 1.\n\n```\n// active = true AND (priority = 1 OR severity = 1)\nvar query = new GlideQuery('incident')\n    .where('active', true)\n    .where(new GlideQuery()\n        .where('priority', 1)\n        .orWhere('severity', 1))\n   .limit(5)\n   .select()\n   .toArray(5)\n\ngs.info(JSON.stringify(query, null, 2));\n```\n\nOutput:\n\n```\n[\n   {\n      \"sys_id\":\"b0f31e5673500010c2e7660c4cf6a711\"\n   },\n   {\n      \"sys_id\":\"8ff5b254b33213005e3de13516a8dcf7\"\n   },\n   {\n      \"sys_id\":\"d999e5fc77e72300454792718a10611d\"\n   },\n   {\n      \"sys_id\":\"30ad318577ab2300454792718a10619e\"\n   },\n   {\n      \"sys_id\":\"3883f4c0730123002728660c4cf6a754\"\n   }\n]\n```\n\n## GlideQuery - whereNotNull\\(String field\\)\n\nReturns records that do not contain a null value in a given field.\n\n**Note:** Do not precede this method with the orWhere\\(\\), orWhereNull\\(\\), or orWhereNotNull\\(\\) methods.\n\n|Name|Type|Description|\n|----|----|-----------|\n|field|String|Field used in the query.|\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis example shows how to query the User table and return results where the first\\_name field is not null.\n\n```\nvar query = new global.GlideQuery('sys_user')\n   .whereNotNull('first_name')\n   .select('name')\n   .limit(5)\n   .forEach(function (user){\n      gs.info(JSON.stringify(user, null, 2));\n   });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"name\": \"System Administrator\",\n  \"sys_id\": \"6816f79cc0a8016401c5a33be04be441\"\n}\n*** Script: {\n  \"name\": \"Alva Pennigton\",\n  \"sys_id\": \"ca826bf03710200044e0bfc8bcbe5d89\"\n}\n*** Script: {\n  \"name\": \"Benchmark Scheduler\",\n  \"sys_id\": \"d3383a875b0132003d1c484c11f91a5b\"\n}\n*** Script: {\n  \"name\": \"Allyson Gillispie\",\n  \"sys_id\": \"06826bf03710200044e0bfc8bcbe5d8a\"\n}\n*** Script: {\n  \"name\": \"SOAP Guest\",\n  \"sys_id\": \"155699460a0a0b2b009e27c10b7f68f6\"\n}\n```\n\n## GlideQuery - whereNull\\(String field\\)\n\nReturns records that contain a null value in a given field.\n\n**Note:** Do not precede this method with the orWhere\\(\\), orWhereNull\\(\\), or orWhereNotNull\\(\\) methods.\n\n|Name|Type|Description|\n|----|----|-----------|\n|field|String|Field used in the query.|\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis example shows how to query the User table and return records where the first or last names are null.\n\n```\nvar query = new global.GlideQuery('sys_user')\n   .whereNull('last_name')\n   .orWhereNull('first_name')\n   .select('first_name', 'last_name')\n   .forEach(function (user){\n      gs.info(JSON.stringify(user, null, 2));\n   });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"first_name\": \"ml.admin\",\n  \"last_name\": null,\n  \"sys_id\": \"88aad6c5c73003005f1b78d48b9763a5\"\n}\n*** Script: {\n  \"first_name\": \"Sitemap Scheduler User\",\n  \"last_name\": null,\n  \"sys_id\": \"85388c25b71011104eed4643ae11a993\"\n}\n*** Script: {\n  \"first_name\": null,\n  \"last_name\": \"Guest\",\n  \"sys_id\": \"5136503cc611227c0183e96598c4f706\"\n}\n*** Script: {\n  \"first_name\": \"ml_report.user\",\n  \"last_name\": null,\n  \"sys_id\": \"b6bad6c5c73003005f1b78d48b97632a\"\n}\n```\n\n## GlideQuery - withAcls\\(\\)\n\nExecutes the query using the GlideRecordSecure API to securely query the database while honoring ACLs.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|GlideQuery|The query object being built.|\n\nThis example shows how to execute a secure query using ACLs.\n\n```\nvar users = new global.GlideQuery('sys_user')\n    .withAcls()\n    .limit(5)\n    .orderByDesc('first_name')\n    .select('first_name')\n    .forEach(function (user){\n        gs.info(JSON.stringify(user, null, 2));\n    });\n```\n\nOutput:\n\n```\n*** Script: {\n  \"first_name\": \"Zane\",\n  \"sys_id\": \"16826bf03710200044e0bfc8bcbe5dbc\"\n}\n*** Script: {\n  \"first_name\": \"Zackary\",\n  \"sys_id\": \"8a826bf03710200044e0bfc8bcbe5d69\"\n}\n*** Script: {\n  \"first_name\": \"Yvette\",\n  \"sys_id\": \"4e826bf03710200044e0bfc8bcbe5d57\"\n}\n*** Script: {\n  \"first_name\": \"Winnie\",\n  \"sys_id\": \"f682abf03710200044e0bfc8bcbe5d1d\"\n}\n*** Script: {\n  \"first_name\": \"Wilmer\",\n  \"sys_id\": \"42826bf03710200044e0bfc8bcbe5d7b\"\n}\n```\n\n"}}