{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1024,"path":"api-reference/server-api-reference/c_GlideRecordUtilAPI.md","title":"GlideRecordUtil - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# GlideRecordUtil- Global\n\nThe GlideRecordUtil script include is a utility class for working with GlideRecords.\n\nThis script include is available in server-side scripts. For use cases, see [Using discovery script includes](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/c_ScriptIncludes.md).\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## GlideRecordUtil - getCIGR\\(String sys\\_id\\)\n\nReturns the GlideRecord object for the specified configuration item \\(CI\\) using just the sys\\_id of the CI.\n\nUse this method to quickly obtain a specific CI without knowing its associated class/table.\n\n|Name|Type|Description|\n|----|----|-----------|\n|sys\\_id|String|Sys\\_id of the desired CI.|\n\n|Type|Description|\n|----|-----------|\n|[GlideRecord](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideRecordAPI.md)|GlideRecord object of the specified CI.|\n\nThe following example returns the GlideRecord object for the specified CI using just the sys\\_id of the CI.\n\n```\nvar now_GR = new GlideRecordUtil().getCIGR(\"2dfd7c8437201000deeabfc8bcbe5d56\");\n```\n\n## GlideRecordUtil - getFields\\(GlideRecord gr\\)\n\nReturns an array of all the fields in the specified GlideRecord.\n\n**Note:** If there is a field name which is the same as the table name, the getFields\\(\\) method does not return the value of the field.\n\n|Name|Type|Description|\n|----|----|-----------|\n|gr|[GlideRecord](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideRecordAPI.md)|GlideRecord instance positioned to a valid record.|\n\n|Type|Description|\n|----|-----------|\n|Array|Field names for the specified GlideRecord.|\n\n```\nvar queryString = \"priority=1^ORpriority=2\";\nvar now_GR = new GlideRecord('incident');\nnow_GR.addEncodedQuery(queryString);\nnow_GR.query();\nnow_GR.next();\n\nvar gRU = new GlideRecordUtil();\nvar fieldList = gRU.getFields(now_GR);\ngs.info(fieldList);\n```\n\nOutput: Line breaks added for presentation.\n\n```\nsys_id,skills,closed_by,assigned_to,contract,category,escalation,state,reassignment_count,location,\ntime_worked,order,due_date,number,upon_approval,sys_tags,sla_due,follow_up,reopen_count,notify,business_stc,\ncaused_by,rejection_goto,assignment_group,comments_and_work_notes,incident_state,opened_at,parent_incident,\nbusiness_service,wf_activity,calendar_duration,group_list,caller_id,comments,priority,sys_updated_by,\nvariables,delivery_task,resolved_at,sys_updated_on,parent,active,opened_by,expected_start,watch_list,\ncompany,upon_reject,work_notes,sys_created_by,additional_assignee_list,approval_set,cmdb_ci,user_input,\nsys_created_on,close_code,contact_type,resolved_by,rfc,approval_history,activity_due,severity,child_incidents,\n,subcategory,work_end,closed_at,close_notes,variables,business_duration,knowledge,approval,sys_domain_path,\nsys_mod_count,problem_id,calendar_stc,work_start,sys_domain,correlation_id,sys_class_name,short_description,\nimpact,description,correlation_display,urgency,made_sla,delivery_plan,work_notes_list\n```\n\n## GlideRecordUtil - getGR\\(String baseTable, String sys\\_id\\)\n\nReturns a GlideRecord instance for the given table, positioned to the given sys\\_id, and of the right class \\(table\\).\n\nThis method is useful when you need to load a GlideRecord from a sys\\_id, but you don't know what the actual table is \\(because it may be extended from the base table\\). This method always returns a GlideRecord of the correct type base\\_table: the name of the base table that the specified sys\\_id is in.\n\n|Name|Type|Description|\n|----|----|-----------|\n|baseTable|String|The name of the base table containing the sys\\_id.|\n|sys\\_id|String|The sys\\_id of the desired record.|\n\n|Type|Description|\n|----|-----------|\n|[GlideRecord](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideRecordAPI.md)|The GlideRecord for the specified sys\\_id.|\n\n```\nvar now_GR = new GlideRecordUtil().getGR(\"cmdb_ci_computer\", \"2dfd7c8437201000deeabfc8bcbe5d56\");\n```\n\n## GlideRecordUtil - getTables\\(String tableName\\)\n\nReturns a list of the ancestors of a specified table.\n\n**Note:** The specified table is included in the list of its ancestors.\n\n|Name|Type|Description|\n|----|----|-----------|\n|tableName|String|Name of the table.|\n\n|Type|Description|\n|----|-----------|\n|Array|List of ancestors of the specified table.|\n\nThe following example shows the ancestors of the Linux Server \\[cmdb\\_ci\\_linux\\_server\\] table.\n\n```\nvar tables = new GlideRecordUtil().getTables(\"cmdb_ci_linux_server\");\ngs.info(\"Tables returned: \" + tables);\n```\n\nOutput:\n\n```\nTables returned: [cmdb_ci_linux_server, cmdb_ci_server, cmdb_ci_computer, cmdb_ci_hardware, cmdb_ci, cmdb]\n\n```\n\n## GlideRecordUtil - mergeToGR\\(Object hashMap, GlideRecord now\\_GR, Object ignore\\)\n\nSets the fields in the specified GlideRecord with the field values contained in the specified hashmap, unless that field name is in the ignore hashmap.\n\n|Name|Type|Description|\n|----|----|-----------|\n|hashMap|Object|An Object instance \\(being used as a hashmap\\), with properties named for fields and containing the fields' value.|\n|now\\_GR|[GlideRecord](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideRecordAPI.md)|The GlideRecord instance to receive the field values.|\n|ignore|Object|An optional hashmap of field names to ignore.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThis example updates a computer record's name and os fields, but does not update the sys\\_created\\_by field:\n\n```\nvar now_GR = new GlideRecordUtil().getGR(\"cmdb_ci_computer\", \"2dfd7c8437201000deeabfc8bcbe5d56\");   \nvar obj = {\"name\": \"xyz\", \"os\": \"windows 2000\", \"sys_created_by\", \"aleck.lin\"};\nvar ignore = {\"sys_created_by\": true};\nnew GlideRecordUtil().mergeToGR(obj, now_GR, ignore);\nnow_GR.update();\n```\n\n## GlideRecordUtil - populateFromGR\\(Object hashMap, GlideRecord now\\_GR, Object ignore\\)\n\nPopulates a given hashmap from a GlideRecord. Each field in the GlideRecord becomes a property in the hashmap.\n\n<table id=\"table_czg_4lw_4t\" 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\nhashMap\n\n</td><td>\n\nObject\n\n</td><td>\n\nThe object to populate with fields and values from a GlideRecord.\n\n</td></tr><tr><td>\n\nnow\\_GR\n\n</td><td>\n\n[GlideRecord](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideRecordAPI.md)\n\n</td><td>\n\nA GlideRecord instance positioned to a valid record.\n\n</td></tr><tr><td>\n\nignore\n\n</td><td>\n\nObject\n\n</td><td>\n\nOptional. A hashmap of field names to exclude when populating the object.For each key-value pair in the map, the key is the name of the field to exclude and the value is `true`.\n\n```\n{\"field_name\": true}\n```\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\nThis example populates an object with fields and values from a Computer \\[cmdb\\_ci\\_computer\\] record.\n\n```\nvar util = new GlideRecordUtil();\nvar objectToPopulate = {};\nvar computer = util.getCIGR(\"2dfd7c8437201000deeabfc8bcbe5d56\"); // get CI record from cmdb_ci_computer\nvar ignore = {\"sys_created_on\": true, \"sys_updated_by\": true};\nutil.populateFromGR(objectToPopulate, computer, ignore);\n// Now objectToPopulate contains field/value pairs from the computer GlideRecord\ngs.info(JSON.stringify(objectToPopulate));\n```\n\nOutput:\n\n```\n{\n   \"sys_id\": \"2dfd7c8437201000deeabfc8bcbe5d56\",\n   \"operational_status\": \"1\",\n   \"os_service_pack\": \"Service Pack 2\",\n   \"cpu_manufacturer\": \"7aad6d00c611228400f00e0f80b67d2d\",\n   \"sys_updated_on\": \"2024-07-27 22:28:37\",\n   \"first_discovered\": \"2007-03-13 20:55:20\",\n   \"invoice_number\": \"INV0166221\",\n   \"sys_created_by\": \"admin\",\n   \"warranty_expiration\": \"2019-10-07\",\n   \"ram\": \"1534\",\n   \"cpu_name\": \"Intel(R) Pentium(R) M processor 1.86GHz\",\n   \"cpu_speed\": \"798\",\n   \"sys_domain_path\": \"/\",\n   \"disk_space\": \"51\",\n   \"cost_center\": \"d9d0a971c0a80a641c20b13d99a48576\",\n   \"assigned\": \"2024-03-16 07:00:00\",\n   \"purchase_date\": \"2018-10-07\",\n   \"last_discovered\": \"2007-04-12 23:17:45\",\n   \"can_print\": \"0\",\n   \"sys_class_name\": \"cmdb_ci_computer\",\n   \"manufacturer\": \"aa0a6df8c611227601cd2ed45989e0ac\",\n   \"cpu_count\": \"1\",\n   \"vendor\": \"aa0a6df8c611227601cd2ed45989e0ac\",\n   \"assigned_to\": \"47de9c73c61122930033f6521d1bb785\",\n   \"os_version\": \"5.1.2600\",\n   \"serial_number\": \"L3BB911\",\n   \"cd_rom\": \"0\",\n   \"unverified\": \"0\",\n   \"asset\": \"8bc1ba8837f3100044e0bfc8bcbe5db9\",\n   \"cpu_core_count\": \"1\",\n   \"skip_sync\": \"0\",\n   \"sys_domain\": \"global\",\n   \"install_date\": \"2023-10-06 08:00:00\",\n   \"asset_tag\": \"P1000227\",\n   \"hardware_status\": \"installed\",\n   \"install_status\": \"1\",\n   \"name\": \"*ASSET-IBM\",\n   \"virtual\": \"0\",\n   \"po_number\": \"PO27711\",\n   \"sys_class_path\": \"/!!/!2/!(\",\n   \"company\": \"a66b1fb03710200044e0bfc8bcbe5d08\",\n   \"department\": \"221f79b7c6112284005d646b76ab978c\",\n   \"cost\": \"3665\",\n   \"os\": \"Windows XP Professional\",\n   \"sys_mod_count\": \"17\",\n   \"monitor\": \"0\",\n   \"model_id\": \"a9a2d0c3c6112276010db16c5ddd3461\",\n   \"cost_cc\": \"USD\",\n   \"location\": \"8225b668ac1d55eb679878e192fca315\",\n   \"category\": \"Do not migrate to asset\",\n   \"fault_count\": \"0\"\n}\n```\n\n"}}