{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":961,"path":"api-reference/server-api-reference/c_CIDataAPI.md","title":"CIData - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# CIData- Global\n\nThe CIData script include is a utility class that provides methods for working with configuration item \\(CI\\) data structures in JavaScript.\n\nThis script include requires the Discovery \\(com.snc.discovery.core\\) plugin. You can use it in any server-side Discovery script.\n\nInstances of this class represent a CI \\(both its base record and any related lists\\), and the provided methods allow loading from or storing both the base record and related lists.\n\nThis class acts as a container for simple classes acting as maps of property name/value pairs. This class contains three kinds of such maps.\n\n-   A single map representing the base CI table entry \\(such as one row cmdb\\_ci\\_linux\\_server and its superclasses\\). You can retrieve this map using the [CIData - getData\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_CIDataAPI.md) method.\n-   Arrays of maps representing the instances of related lists, with each array representing a single related list and each element of such an array representing a single row of that related list. These arrays can be retrieved with the [CIData - getRelatedList\\(String table, String refField\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_CIDataAPI.md) method.\n-   Arrays of maps representing the instances of many-to-many tables, with each array representing a single many-to-many table and each element of such an array representing a single row of that many-to-many table. These arrays can be retrieved with the [CIData - getM2MTable\\(String table, String refField\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_CIDataAPI.md) method.\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## CIData - CIData\\(String debugFlag\\)\n\nCreates an instance of the CIData class.\n\n<table id=\"table_sjx_wn1_rt\" 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\ndebugFlag\n\n</td><td>\n\nString\n\n</td><td>\n\nFlag that indicates whether to enable debug logging.Valid values:\n\n-   True: Debug logging is enabled.\n-   False: Debug logging is not enabled.\n\n</td></tr></tbody>\n</table>This example creates and adds a related list to the CIData object.\n\n```\nvar ciDataObj = new CIData();\nvar rl = new CIRelatedList('cmdb_serial_number', 'cmdb_ci');\nvar sr = {};\nsr['serial_number_type'] = \"bios\";\nsr['serial_number'] = \"1BC5E4z89246\";\nsr['valid'] =  \"true\";\nrl.addRec(sr);\nciDataObj.addRelatedList(rl);\n```\n\n## CIData - addRelatedList\\(Array relatedList\\)\n\nAdds the specified related list to this instance.\n\n|Name|Type|Description|\n|----|----|-----------|\n|relatedList|Array|Related list to add.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThis example creates and adds a related list to the CIData object.\n\n```\nvar ciDataObj = new CIData();\nvar rl = new CIRelatedList('cmdb_serial_number', 'cmdb_ci');\nvar sr = {};\nsr['serial_number_type'] = \"bios\";\nsr['serial_number'] = \"1BC5E4z89246\";\nsr['valid'] =  \"true\";\nrl.addRec(sr);\nciDataObj.addRelatedList(rl);\n```\n\n## CIData - convertRelatedList\\(String sensor, String tableName, String refField, String keyName\\)\n\nConverts the specified related list to a related list in a given sensor.\n\n|Name|Type|Description|\n|----|----|-----------|\n|sensor|String|The sensor|\n|tableName|String|The table name|\n|refField|String|The reference field|\n|keyName|String|The key name|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n## CIData - fromXML\\(String xml\\)\n\nInitializes the current CIData instance from the specified XML string.\n\n|Name|Type|Description|\n|----|----|-----------|\n|xml|String|An XML string.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThe following example shows how to get test XML-formatted CI Data collected during discovery.\n\n```\nvar xmlCIData = '<CIData><data><fld name=\"cpu_name\">Intel(R) Xeon(R) CPU E5-2686 v4 @ 2.30GHz</fld><fld name=\"host_name\">ip-172-31-19-95</fld><fld name=\"kernel_release\">3.10.0-1160.45.1.el7.x86_64</fld><fld name=\"os\">Linux CentOS</fld></data></CIData>';\n\nvar ciData = new CIData();\n\nciData.fromXML(xmlCIData);\ngs.log(ciData);\n```\n\nOutput:\n\n```\nCIData instance:\n  cpu_name: Intel(R) Xeon(R) CPU E5-2686 v4 @ 2.30GHz\n  host_name: ip-172-31-19-95\n  kernel_release: 3.10.0-1160.45.1.el7.x86_64\n  os: Linux CentOS\n[object Object]\n```\n\n## CIData - getData\\(\\)\n\nGets the data map in the base CI record.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Object|The data map|\n\n## CIData - getM2MTable\\(String table, String refField\\)\n\nReturns an array of data maps in the given many-to-many list \\(to this CI\\).\n\n|Name|Type|Description|\n|----|----|-----------|\n|table|String|Name of the table containing the related list. In the case of a many-to-many list, this is the name of the many-to-many table, not the target table.|\n|refField|String|Name of the field in the related list that refers to this CI. In the case of a many-to-many list, this is the name of the referring field in the many-to-many table, not in the target table.|\n\n<table id=\"table_rzb_hv1_rt\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nArray\n\n</td><td>\n\nList of data maps in the given many-to-many list. The list is not in any particular order.If this is not a many-to-many list, returns an empty array.\n\n</td></tr></tbody>\n</table>This example shows how to get the many-to-many relationships for the installed\\_on field of the cmdb\\_software\\_instance table.\n\n```\nfunction () {\n  //Instantiate a new CIData class object\n  var cdta = new CIData();\n\n  //Load the current data from a CI record.\n  cdta.loadFromCI('b4fd7c8437201000deeabfc8bcbe5dc1');\n\n  // Returns an Array of Objects (records) of a given M2M related list\n  var records = cdta.getM2MTable('cmdb_software_instance', 'installed_on');\n\n  gs.info(JSON.stringify(records, null, 4));\n\n})();\n```\n\nOutput:\n\n```\n[\n  {\n    \"sys_id\": \"208983661b3330106cdf0f26624bcb4b\",\n    \"software\": \"46c1ec3ca9fe198100beafe77332be9a\",\n    \"sys_updated_on\": \"2021-11-16 18:32:35\",\n    \"sys_created_on\": \"2021-11-16 18:32:35\",\n    \"name\": \"3D Groove Playback Engine-*ANNIE-IBM\",\n    \"installed_on\": \"b4fd7c8437201000deeabfc8bcbe5dc1\"\n  },\n  {\n    \"sys_id\": \"288983661b3330106cdf0f26624bcb4a\",\n    \"software\": \"0c43bc5fc61122750182c13269a7a3ef\",\n    \"sys_updated_on\": \"2021-11-16 18:32:35\",\n    \"sys_created_on\": \"2021-11-16 18:32:35\",\n    \"name\": \".NET SDK 2003-*ANNIE-IBM\",\n    \"installed_on\": \"b4fd7c8437201000deeabfc8bcbe5dc1\"\n  }\n]\n```\n\n## CIData - getRelatedList\\(String table, String refField\\)\n\nGets an array of maps of data in the given related list \\(to this CI\\).\n\nThe array is not in any particular order. In the case of a many-to-many list, this will be an array of instances of the target table, not the many-to-many table. For example, given 'cmdb\\_software\\_instance' and 'installed\\_on' \\(a many-to-many table and the field that refers to a CI\\), this method will return an array of maps representing cmdb\\_ci\\_spkg \\(the target table\\) instances.\n\n|Name|Type|Description|\n|----|----|-----------|\n|table|String|Name of the table containing the related list. In the case of a many-to-many list, this is the name of the many-to-many table, not the target table.|\n|refField|String|Name of the field in the related list that refers to this CI. In the case of a many-to-many list, this is the name of the referring field in the many-to-many table, not in the target table.|\n\n|Type|Description|\n|----|-----------|\n|Array|An array of maps of data in the specified related list.|\n\n## CIData - getRelatedListInstance\\(String table, String refField\\)\n\nReturns the instance of CIRelatedList for the given list.\n\n|Name|Type|Description|\n|----|----|-----------|\n|table|String|Name of the table containing the related list. In the case of a many-to-many list, this is the name of the many-to-many table, not the target table.|\n|refField|String|Name of the field in the related list that refers to this CI. In the case of a many-to-many list, this is the name of the referring field in the many-to-many table, not in the target table.|\n\n|Type|Description|\n|----|-----------|\n|Object|Instance of the `CIRelatedList` for the specified list.|\n\nThe following code example uses the getRelatedListInstance\\(\\) method to find the related list relationships to remove.\n\n```\nremoveAllRelatedList: function(ci_id) {\n  var cdta = new CIData('false');\n  cdta.init();\n  cdta.loadFromCI(ci_id); // Get a Computer CI Instance\n  var cirelatedlist = cdta.getRelatedListInstance('cmdb_rel_ci', 'parent'); // Get the CIRelatedList instance holding all relationships of the above CI.\n  cirelatedlist.remove(); // Remove all the CI Relationships\n}\n```\n\n## CIData - init\\(\\)\n\nInitializes this instance in preparation for loading a new CI.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar cdta = CIData('false');\ncdta.init();\n```\n\n## CIData - loadFromCI\\(String cmdb\\_ci\\)\n\nLoads the current data from a specified sys\\_id.\n\n|Name|Type|Description|\n|----|----|-----------|\n|cmdb\\_ci|String|The sys\\_id of the CI|\n\n|Type|Description|\n|----|-----------|\n|Object|The current data from the specified CI.|\n\n```\nvar cdta = CIData('false');\ncdta.init();\ncdta.loadFromCI('ccaf9c0a8016400b98a06818d57c7');\n```\n\n## CIData - toString\\(\\)\n\nConverts the name of this CIData instance to a string.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|Name of the instance|\n\nThis example loads the CIData from the current CI and converts this new CIData instance into string format.\n\n```\nvar cidata = new CIData(); // Instantiate a new CIData class object\ncidata.loadFromCI('0c43b0f5c6112275019abd2bb3dcd78f'); // Load the current data from a CI record. Use sys_id or ci name.\ncidata.toString(); // Convert the name of this CIData instance to a string.\ngs.info(cidata); // Display name value pairs of the current CI\n```\n\nOutput:\n\n```\nCIData instance:\n  sys_id: 0c43b0f5c6112275019abd2bb3dcd78f\n  operational_status: 1\n  os_service_pack: Service Pack 1\n  cpu_manufacturer: 0c43b088c6112275011a4bd46a4e6cc4\n  sys_updated_on: 2022-01-11 07:44:05\n  discovery_source: Other Automated\n  ram: 503\n  cpu_speed: 1993\n  sys_domain_path: /\n  disk_space: 37.26\n  cost_center: d9d07bddc0a80a647cf932056ed24652\n  assigned: 2020-08-31 08:00:00\n  cd_speed: -1\n  can_print: 0\n  sys_class_name: cmdb_ci_computer\n  manufacturer: add7b97737e3100044e0bfc8bcbe5d96\n  cpu_count: 1\n  vendor: adb3d8ce37413000158bbfc8bcbe5d15\n  assigned_to: 5137153cc611227c000bbd1bd8cd2007\n  os_version: 5.1.2600\n  cd_rom: 0\n  unverified: 0\n  asset: aac1ba8837f3100044e0bfc8bcbe5d3b\n  skip_sync: 0\n  sys_created_on: 2005-05-24 01:14:19\n  sys_domain: global\n  cpu_type: GenuineIntel\n  install_date: 2020-05-19 07:00:00\n  asset_tag: P1000143\n  install_status: 1\n  name: ANDREWDWXP\n  subcategory: Computer\n  virtual: 0\n  sys_class_path: /!!/!2/!(\n  company: 31bea3d53790200044e0bfc8bcbe5dec\n  department: 221f3db5c6112284009f4becd3039cc9\n  cost: 1299.99\n  os: Windows XP Professional\n  attestation_status: Not Yet Reviewed\n  monitor: 0\n  ip_address: 195.11.1.1\n  cost_cc: USD\n  location: db9a923c0a0a0a6501068d6eaec25ee0\n  category: Hardware\n  fault_count: 0\n[object Object]\n\n```\n\n## CIData - toXML\\(\\)\n\nReturns an XML string containing a serialized version of this instance \\(including any related lists\\).\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|The XML string containing a serialized version of this instance and any related lists.|\n\n```\nvar cdta = CIData();\ncdta.toXML();\n```\n\n"}}