{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":192,"path":"api-reference/c_GlideRecordClientSideAPI.md","title":"GlideRecord - Client","bundle":null,"doc_type":null,"release":"australia","body":"\n# GlideRecord- Client\n\nThe GlideRecord API provides methods that perform database operations. This API enables the use of some GlideRecord functionality in client-side scripts, such as client scripts and UI policy scripts.\n\nA GlideRecord contains both records and fields. Queries made with client-side GlideRecord methods are executed on the server. Therefore, a request is made from the client browser to obtain the record data.\n\nThe client-side GlideRecord API is not supported in scoped applications. Instead, create a script include and use the [GlideAJAX](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/c_GlideAjaxAPI.md) API, or use the REST APIs. In addition, this API applies access control lists \\(ACLs\\) based on the credentials of the user executing the script. To execute the code on the server without ACLs, use the [GlideAjax](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/c_GlideAjaxAPI.md) API.\n\n**Parent Topic:**[Client API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/api-client.md)\n\n## Client side GlideRecord - GlideRecord\\(String tableName\\)\n\nCreates an instance of the GlideRecord class for the specified table.\n\n|Name|Type|Description|\n|----|----|-----------|\n|tableName|String|The table to be used.|\n\n```\nvar now_GR = new GlideRecord('incident');\n```\n\n## Client side GlideRecord - addOrderBy\\(String column\\)\n\nAdds a column to order by in the query.\n\n|Name|Type|Description|\n|----|----|-----------|\n|column|String|The column by which to order the result set.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n## Client side GlideRecord - addQuery\\(String name, Object value\\)\n\nAdds a query to return records where the specified field name is equal to a specified value \\(or is in a list of values\\).\n\nIf you are familiar with SQL, this method is similar to the \"where\" clause. You can create one or more queries for a single filter by calling this method multiple times; for this method the queries are AND'ed. Once you define all of the desired queries, call the [Client side GlideRecord - query\\(String name, Function responseFunction, String value\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/c_GlideRecordClientSideAPI.md) to execute the specified query clause \\(filter\\).\n\nTo perform an operation other than AND, use either the [addQuery\\(String name, Object operator, Object value\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/c_GlideRecordClientSideAPI.md) method or the [setEncodedQuery\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/c_GlideRecordClientSideAPI.md) method.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|Name of the field to check.|\n|value|Object|The value or list of values on which to query.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nExample showing multiple queries.\n\n```\nvar now_GR = new GlideRecord('incident');\nnow_GR.addQuery('priority', 4); // Priority is 4 – Low and,\nnow_GR.addQuery('state', 3); // State is On Hold\nnow_GR.query(response);\n \nfunction response(result) {\n    while(result.next()) {\n       // Print all INC with priority 4 – Low AND state is On Hold\n        console.log(result.getValue('number')); \n    }\n}\n```\n\nExample showing how to pass a string object.\n\n```\nvar now_GR = new GlideRecord('incident');\nnow_GR.addQuery('short_description', new String('USB device not working')); // string object\nnow_GR.addQuery('priority', 4); //number\nnow_GR.query(response);\n \nfunction response(result) {\n    while(result.next()) {\n       // Print all INC with priority 4 – Low AND short description contains 'USB device not working'\n        console.log(result.getValue('number')); \n    }\n}\n```\n\n## Client side GlideRecord - addQuery\\(String name, Object operator, Object value\\)\n\nAdds a filter to return records where the field meets the specified condition \\(field, operator, value\\).\n\nIf you are familiar with SQL, this method is similar to the \"where\" clause. You can create one or more queries for a single filter by calling this method multiple times. Once you define all of the desired queries, call the [Client side GlideRecord - query\\(String name, Function responseFunction, String value\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/c_GlideRecordClientSideAPI.md) to execute the specified query clause \\(filter\\).\n\nTo create more complex queries, use the [setEncodedQuery\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/c_GlideRecordClientSideAPI.md) method.\n\n<table id=\"table_wgh_cpv_hv\" 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\nname\n\n</td><td>\n\nString\n\n</td><td>\n\nName of the field to check.\n\n</td></tr><tr><td>\n\noperator\n\n</td><td>\n\nObject\n\n</td><td>\n\nQuery operator. The available values are dependent on the data type of the *value* parameter.Numbers:\n\n-   =\n-   !=\n-   &gt;\n-   &gt;=\n-   &lt;\n-   &lt;=\n\n Strings \\(must be in upper case\\):\n\n-   =\n-   !=\n-   IN\n-   NOT IN\n-   STARTSWITH\n-   ENDSWITH\n-   CONTAINS\n-   DOES NOT CONTAIN\n-   INSTANCEOF\n\n**Note:** Don't use the LIKE operator. Use CONTAINS instead.\n\n</td></tr><tr><td>\n\nvalue\n\n</td><td>\n\nObject\n\n</td><td>\n\nValue on which to query \\(not case-sensitive\\).\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\nExample showing how to add multiple queries to a filter.\n\n```\nvar now_GR = new GlideRecord('incident');\nnow_GR.addQuery('priority', '<=', 2); // Priority is 2 or higher and,\nnow_GR.addQuery('short_description', 'CONTAINS', 'crash'); // Short description contains the word crash\nnow_GR.query(response);\n \nfunction response(result) {\n    while(result.next()) {\n       // Print all INC with priority of 2 or higher AND short description contains \"crash\"\n        console.log(result.getValue('number')); \n    }\n}\n```\n\nExample showing how to pass in an array to verify multiple conditions in a single query.\n\n```\nvar priorities = [4,2]; \nvar now_GR = new GlideRecord('incident'); \nnow_GR.addQuery('priority', 'IN', priorities); \nnow_GR.query(response); \n\nfunction response(result) {\n  while(result.next()) {\n    console.log(result.getValue('number')); \n  } \n}\n```\n\n## Client side GlideRecord - deleteRecord\\(Function responseFunction\\)\n\nDeletes the current record and calls the specified response function when complete.\n\n|Name|Type|Description|\n|----|----|-----------|\n|responseFunction|Function|Response function for the callback.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThis example deletes a record and then calls the response function *response* to log an alert message.\n\n```\nvar recordGR = new GlideRecord('incident');\nif (recordGR.get('99ebb4156fa831005be8883e6b3ee4b9')) {\n  recordGR.deleteRecord(response);\n}\n\nfunction response(result) {\n  alert('Deleted record sys_id: ' + result.getValue('sys_id'));\n}\n```\n\nOutput:\n\n```\nDeleted record sys_id: 99ebb4156fa831005be8883e6b3ee4b9\n```\n\n## Client side GlideRecord - get\\(String sys\\_id\\)\n\nExecutes a GlideRecord query for a record with the specified sys\\_id. This method is expected to be used to query for single records, so a next operation is performed before returning.\n\n|Name|Type|Description|\n|----|----|-----------|\n|sys\\_id|String|The sys\\_id of the record to be found.|\n\n|Type|Description|\n|----|-----------|\n|Boolean|True if one or more matching records was found. False if no records were found.|\n\n## Client side GlideRecord - getEncodedQuery\\(\\)\n\nRetrieves the query condition of the current result set as an encoded query string.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|An encoded query string containing all conditions that have been added to the query.|\n\n## Client side GlideRecord - getLimit\\(\\)\n\nReturns the limit for records to be returned by the GlideRecord query.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Number|The limit for records to be returned by the GlideRecord query.|\n\n## Client side GlideRecord - getTableName\\(\\)\n\nRetrieves the name of the table associated with this GlideRecord.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|The table name|\n\n```\nvar item = new GlideRecord('sc_request'); \nitem.addQuery('sys_id', current.request); \nitem.query(itemResponse); \n \nfunction itemResponse(item) {\n   alert('The table is ' + item.getTableName());\n}\n```\n\n## Client side GlideRecord - hasNext\\(\\)\n\nDetermines if there are any more records in the GlideRecord.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Boolean|True if there are more records in the query set.|\n\n## Client side GlideRecord - insert\\(Function responseFunction\\)\n\nInserts a new record using the field values that have been set for the current record.\n\n|Name|Type|Description|\n|----|----|-----------|\n|responseFunction|Function|Function to execute once the record is inserted.|\n\n|Type|Description|\n|----|-----------|\n|String|Sys\\_id of the inserted record, or null if the record was not inserted.|\n\n```\nvar now_GR = new GlideRecord('incident');\nnow_GR.short_description = 'Learn about GlideRecord';\nvar recResponse = now_GR.insert(handleResponse);\n\nfunction handleResponse(recResponse, answer) {\n// Answer will be the sys_id of the created record or null\nalert('Newly created sys_id is - ' + answer + ' exists');\n}\n```\n\n## Client side GlideRecord - next\\(\\)\n\nMoves to the next record in the GlideRecord.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Boolean|False if there are no more records in the query set.|\n\n```\nvar rec = new GlideRecord('incident');\nrec.query(recResponse);\n \nfunction recResponse(rec) {\n  while (rec.next()) { \n    alert(rec.number + ' exists');\n  }\n}\n```\n\n## Client side GlideRecord - orderBy\\(String column\\)\n\nSpecifies an orderBy column. May be called more than once to order by multiple columns.\n\n|Name|Type|Description|\n|----|----|-----------|\n|column|String|The column name to be used to order the result set.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nfunction UpdateProjectWBS(project) {\n  var count = 0;\n  var child = new GlideRecord('pm_project_task');\n  child.addQuery('parent', project.sys_id);\n  child.orderBy('order');\n  child.orderBy('number');\n  child.query(childResponse);\n  g_form.addInfoMessage(count + ' Project Tasks updated');\n}\n \nfunction childResponse(child) {\n  var len = child.getRowCount().toString().length;\n  var seq = 0;\n  while (child.next()) {\n    count += UpdateProjectTaskWBS(child, 1, ++seq, len, '');\n  }\n}\n```\n\n## Client side GlideRecord - query\\(String name, Function responseFunction, String value\\)\n\nRuns the query to the server against the table based on the addQuery\\(\\) filter. This method queries the GlideRecord table as well as any references of the table.\n\nDo not make synchronous query calls. Performing a query without a response function makes the call synchronous, which means that the display will wait for the query response before continuing.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|Optional. The name of a field to query.|\n|responseFunction|Function|The function called when the query results are available.|\n|value|String|Optional. The field value to query for. Any pair of literals is considered a query pair \\(field : value\\).|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThe following is a basic example with a response function.\n\n```\nvar rec = new GlideRecord('incident');\nrec.query(recResponse);\n \nfunction recResponse(rec) {\n  while (rec.next()) { \n   alert(rec.number + ' exists');\n  } \n}\n```\n\nThe following example shows the difference between synchronus and asynchronous syntax, with an asynchronous example.\n\n```\n// synchronous call syntax (no response function): DO NOT USE\n        query();\n        \n        // asynchronous call syntax\n        // performs query with current conditions, calls responseFunction when done\n        query(responseFunction)\n        \n        // synchronous call syntax (no response function): DO NOT USE\n        // adds \"category=hardware\" to current query conditions and performs query\n        query('category', 'hardware')\n        \n        // asynchronous call syntax\n        // adds \"category=hardware\" to current query conditions, performs query, and calls responseFunction when done\n        query('category', 'hardware', responseFunction)\n        \n        // asynchronous call example\n        // adds \"user_name=abel.tuter\" to current query conditions, performs query, and calls defined response function when done\n        function onLoad() {\n        var now_GR = new GlideRecord(\"sys_user\");\n        now_GR.query(\"user_name\", \"abel.tuter\", function(now_GR) {\n        if (now_GR.next()) {\n        alert(\"You can access fields by name from the client API, just like in the server API:\\n now_GR.name = \" + now_GR.name);\n        alert(\"You can also access fields using getValue():\\n now_GR.getValue(\\\"email\\\") = \" + now_GR.getValue(\"email\"));\n        if (now_GR.getDisplayValue) {\n        alert(\"In Service Portal, Mobile, and Agent Workspace, you can access a field's display value:\\n now_GR.getDisplayValue(\\\"company\\\") = \" + now_GR.getDisplayValue(\"company\"));\n        } else {\n        alert(\"On the desktop, you cannot access a field's display value, but can get its sys_id:\\n now_GR.company = \" + now_GR.company);\n        }\n        alert(\"You cannot dot-walk in the client API:\\n now_GR.company.name = \" + now_GR.company.name);\n        }\n        });\n        }\n```\n\n## Client side GlideRecord - setEncodedQuery\\(String encodedQuery\\)\n\nAdds a specified encoded query string to the current query clause.\n\nThis method enables you to specify complex filters \\(encoded query strings\\) in a single query call, unlike other client-side addQuery\\(\\) methods. Once you define all of the desired queries, call the [Client side GlideRecord - query\\(String name, Function responseFunction, String value\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/c_GlideRecordClientSideAPI.md) to execute the specified query clause \\(filter\\). For additional information on encoded query strings, refer to [Encoded query strings](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-user-interface/c_EncodedQueryStrings.md).\n\nIf you call this method multiple times before calling the query\\(\\) method, the queries are AND'ed together.\n\n|Name|Type|Description|\n|----|----|-----------|\n|encodedQuery|String|Encoded query string to add to the current query clause.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar now_GR = new GlideRecord('incident');\nnow_GR.setEncodedQuery('priority=4^ORstate=3');\nnow_GR.query(response);\n \nfunction response(result) {\n    while(result.next()) {\n        console.log(result.getValue('number'));\n    }\n}\n\n```\n\n## Client side GlideRecord - setLimit\\(Number maxQuery\\)\n\nSets the limit for how many records are in the GlideRecord.\n\n|Name|Type|Description|\n|----|----|-----------|\n|maxQuery|Number|The limit for the number of records to retrieve.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n"}}