{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":968,"path":"api-reference/server-api-reference/c_CMDBUtilAPI.md","title":"CMDBUtil - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# CMDBUtil- Global\n\nThe CMDBUtil API provides utility methods for creating and managing table relationships in the Configuration Management Database \\(CMDB\\) and managing CMDB baselines.\n\nCMDBUtil is a JavaScript-accessible ScriptableObject. The CMDBUtil API has dynamic and static methods. You access dynamic methods by creating a SNC.CMDBUtil object. You access static methods by using SNC.CMDBUtil global object to call the methods.\n\nThis example creates a CMDBUtil object.\n\n```\nvar cu = new SNC.CMDBUtil();\ncu.baselineProposedChangesGenDIFF(current, action.get('sysparm_changeset'));\n```\n\nThis example calls a static method.\n\n```\nvar output = SNC.CMDBUtil.getAllChildrenOfAsCommaList('cmdb_ci_computer');\n```\n\nUse these methods to manage CMDB table relationships and baselines.\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## CMDBUtil - baselineProposedChangesApplyChanges\\(GlideRecord proposedChanges\\)\n\nApplies proposed changes in a task\\_ci record that represent an update set for all CIs associated with the task.\n\nThis is a dynamic method.\n\n|Name|Type|Description|\n|----|----|-----------|\n|proposedChanges|GlideRecord|The collection of proposed changes in the change request \\(CHG\\).|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\n//Where current is a GlideRecord and action is the current UI action\nvar base = new SNC.CMDBUtil();\nbase.baselineProposedChangesApplyChanges(current);\n```\n\n## CMDBUtil - baselineProposedChangesGenDiff\\(GlideRecord current, String changeSet\\)\n\nGenerates the XML for proposed changes diff, and adds it to the corresponding task\\_ci record.\n\nThis is a dynamic method.\n\n|Name|Type|Description|\n|----|----|-----------|\n|current|GlideRecord|The GlideRecord that contains the collection of proposed changes.|\n|changeSet|String|The sysid of the task\\_ci record that represents the change set to use.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\n//Where current is a GlideRecord and action is the current UI action\nvar base = new SNC.CMDBUtil();\nbase.baselineProposedChangesGenDIFF(current, action.get('sysparm_changeset'));\n```\n\n## CMDBUtil - bootstrap\\(String dictFile\\)\n\nBootstraps the environment by creating the database, creating the system dictionary table, and having the system dictionary table describe itself.\n\nThis is a static method.\n\n|Name|Type|Description|\n|----|----|-----------|\n|dictFile|String|The file name including path, of the dictionary to load.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n## CMDBUtil - createCIRelationship\\(String tableName, String parent, String child, String parentField, String childField\\)\n\nCreates a CI relationship between fields.\n\n<table id=\"table_a1b_4kc_3v\" 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\ntableName\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. The name of the relationship target table.Default: CI Relationship \\[cmdb\\_rel\\_ci\\] table.\n\n</td></tr><tr><td>\n\nparent\n\n</td><td>\n\nString\n\n</td><td>\n\nSys\\_id of the parent CI record in the Configuration Items \\[cmdb\\_ci\\] table.\n\n</td></tr><tr><td>\n\nchild\n\n</td><td>\n\nString\n\n</td><td>\n\nSys\\_id of the child CI record in the Configuration Items \\[cmdb\\_ci\\] table.\n\n</td></tr><tr><td>\n\nparentField\n\n</td><td>\n\nString\n\n</td><td>\n\nThe parent relationship label listed in the CI Relationship Type \\[cmdb\\_rel\\_type\\] table.\n\n</td></tr><tr><td>\n\nchildField\n\n</td><td>\n\nString\n\n</td><td>\n\nThe child relationship label listed in the CI Relationship Type \\[cmdb\\_rel\\_type\\] table.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|String|Sys\\_id of the new record listed in the CI Relationship \\[cmdb\\_rel\\_ci\\] table.|\n\nThe following example shows how to create a CI relationship between the parent CI contains field and the child CI contained\\_by field. The relationship is listed in the CI Relationship \\[cmdb\\_rel\\_ci\\] table.\n\n```\nvar base = new SNC.CMDBUtil();\nbase.createCIRelationship('<parent_ci_sys_id>', '<child_ci_sys_id>, 'Contains', 'Contained by');\n```\n\n## CMDBUtil - getAllChildrenOfAsCommaList\\(String baseTable\\)\n\nGets all the child tables of the specified table as a comma-separated list.\n\nThis is a static method.\n\n|Name|Type|Description|\n|----|----|-----------|\n|baseTable|String|The base table name.|\n\n|Type|Description|\n|----|-----------|\n|String|A comma-separated list of sys\\_ids of tables extending the base table.|\n\n```\n//Where cmdv_ci_computer is a table.\nvar output = SNC.CMDBUtil.getAllChildrenOfAsCommaList('cmdb_ci_computer');\ngs.print(output);\n```\n\n## CMDBUtil - getCMDBViews\\(\\)\n\nGets all the records in the CMDB View \\[cmdb\\_view\\] table.\n\nThis method is dynamic.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Array|List of the records in the CMDB View \\[cmdb\\_view\\] table.|\n\nThe following example shows how to list the CMDB views.\n\n```\nvar util = new SNC.CMDBUtil(); \nvar cmdbViews = util.getCMDBViews();\ngs.info(cmdbViews);\n\n// Note that the output in the example is type: java.util.ArrayList\n// To convert to JS Array, use ArrayUtils (add these lines preceding gs.info):\n// var arrUtil = new global.ArrayUtil();\n// var arrViews = arrUtil.convertArray(cmdbViews);\n```\n\nOutput:\n\n```\n[View 3, View 1, View 2, Another view]\n```\n\n## CMDBUtil - getTables0\\(String tableName\\)\n\nGets a list of all the parents of a table.\n\nThis method is a static method.\n\n|Name|Type|Description|\n|----|----|-----------|\n|tableName|String|The table name.|\n\n|Type|Description|\n|----|-----------|\n|Array|List of tables that are parents of the specified table.|\n\nThe following example shows how to list the parent tables of the Server \\[cmdb\\_ci\\_server\\] table.\n\n```\nvar gr = new GlideRecord('incident');\n\n// Querying the incident table\ngr.addQuery('number','INC0000005'); \ngr.query();\n\n\nif(gr.next()){\n  // Find the list of parent tables for Configuration item field of this Incident\n  var ciParent = SNC.CMDBUtil.getTables0(gr.cmdb_ci.sys_class_name); \n  gs.print(\"Parent Tables for the Configuration Item '\" + gr.cmdb_ci.name + \"' belonging to the CMDB Class '\"+ gr.cmdb_ci.sys_class_name+ \"' are: \\n\" + ciParent);\n}\n```\n\nOutput:\n\n```\nParent Tables for the Configuration Item 'ApplicationServerPeopleSoft' belonging to the CMDB Class 'cmdb_ci_server' are: \n[cmdb_ci_server, cmdb_ci_computer, cmdb_ci_hardware, cmdb_ci, cmdb]\n```\n\n## CMDBUtil - isExcludedFromBR\\(String className\\)\n\nDetermines whether a CI class is defined in the Business Rule Exclusion Lists \\[cmdb\\_business\\_rule\\_exclusions\\] table. Use this method in an advanced condition to prevent a business rule from executing on excluded CI classes.\n\nFor example, the Create Asset on insert business rule uses this method in the advanced condition to prevent the rule from creating assets for CI classes such as cmdb\\_ci\\_qualifier, cmdb\\_ci\\_endpoint, cmdb\\_ci\\_storage\\_volume, and cmdb\\_ci\\_vcenter\\_datastore\\_disk. Define classes for exclusion in the Business Rule Exclusion Lists \\[cmdb\\_business\\_rule\\_exclusions\\] table.\n\n|Name|Type|Description|\n|----|----|-----------|\n|className|String|Name of the class to check for exclusion.|\n\n|Type|Description|\n|----|-----------|\n|Boolean|True if the class is defined in the Business Rule Exclusion Lists \\[cmdb\\_business\\_rule\\_exclusions\\] table; otherwise, false.|\n\n```\n//Returns true if the cmdb_ci_endpoint class is defined \n//in the Business Rule Exclusion Lists table\nvar exclusion = SNC.CMDBUtil.isExcludedFromBR(\"cmdb_ci_endpoint\");\n\n//Advanced condition that prevents business rules from executing on \n//classes in the Business Rule Exclusion Lists table\n(!SNC.CMDBUtil.isExcludedFromBR(current.getTableName()))\n```\n\n## CMDBUtil - processCIChange\\(GlideRecord event, GlideRecord target\\)\n\nWraps the call to RelationshipEventProcessor\\(\\), which processes any changes to CI relationships.\n\nThis is a dynamic method.\n\n|Name|Type|Description|\n|----|----|-----------|\n|event|GlideRecord|The event record.|\n|target|GlideRecord|The target record.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n## CMDBUtil - processRelChange\\(GlideRecord event, GlideRecord current, String relType, String triplet\\)\n\nWraps the call to RelationshipEventProcessor\\(\\), which processes any changes to CI relationships, with the specified type and triplet.\n\nThis is a dynamic method.\n\n|Name|Type|Description|\n|----|----|-----------|\n|event|GlideRecord|The event record|\n|current|GlideRecord|The current record, which is either the relation record or a user record if the current process is a deletion.|\n|relType|String|The type of relation that changed.|\n|triplet|String|The child, parent, and class name from the relation that changed.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n## CMDBUtil - removeCIRelationship\\(String tableName, String parentField, String childField, String parentDesc, Object childDesc\\)\n\nDeletes the specified CI relationship.\n\nThis is a dynamic method.\n\n<table id=\"table_a1b_4kc_3v\" 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\ntableName\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. Table nameDefault: CI Relationship \\[cmdb\\_rel\\_ci\\] table\n\n</td></tr><tr><td>\n\nparentField\n\n</td><td>\n\nString\n\n</td><td>\n\nParent field\n\n</td></tr><tr><td>\n\nchildField\n\n</td><td>\n\nString\n\n</td><td>\n\nChild field\n\n</td></tr><tr><td>\n\nparentDesc\n\n</td><td>\n\nString\n\n</td><td>\n\nParent relationship descriptor\n\n</td></tr><tr><td>\n\nchildDesc\n\n</td><td>\n\nObject\n\n</td><td>\n\nChild relationship descriptor\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\nThis code example shows how to remove every relation type 'Depends on::Used by' for the specified parent.\n\n```\nvar cu = new SNC.CMDBUtil();\nvar parentSysID = '27d18f6fc0a8000b00ad14d4929d1b45';\nvar parentDesc = 'Depends on';\nvar childDesc = 'Used by'\n \n// Get records with parent equal parentSysID\nvar relation = new GlideRecord('cmdb_rel_ci');\nrelation.addQuery('parent', parentSysID);\nrelation.query();\nwhile(relation.next()){\n    // Remove every relation of type: 'Depends on::Used by' for the parent\n    // If the relation type does not exist, it inserts this relation type into [cmdb_rel_type].\n    cu.removeCIRelationship('cmdb_rel_ci', parentSysID, relation.child.getValue('sys_id'), parentDesc, childDesc)\n}\n```\n\n## CMDBUtil - reParentTable\\(String table, String oldParent, String newParent\\)\n\nChanges the parent of the given table to the new parent.\n\n|Name|Type|Description|\n|----|----|-----------|\n|table|String|The table to re-parent.|\n|oldParent|String|The old parent.|\n|newParent|String|The new parent.|\n\n<table id=\"table_hdq_m5c_3v\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates if the parent change is successful.\n\n Valid values:\n\n-   true: Parent change was successful.\n-   false: Parent change wasn't successful.\n\n</td></tr></tbody>\n</table>To recreate the following example, use the code provided to create a custom table. Next create a record and check it in the task table. The custom table created extends from the Task table, which means it has successfully re-parented. The code isn't intended to be directly used via copy and paste. To show output, the custom table was created before running the code.\n\n```\n/*\n  Before running the code in the background Script Make Sure 'Record For Rollback' is unchecked.\n\n  This API CMDBUtil.reParentTable changes the parent of the table but will not allow the OOB tables to change the parent it works only in the custom table. Hence this script contains the creation of the custom table.\n  API CMDBUtil.reParentTable has 3 parameters (Table, Old Parent , New Parent);\n  We can make the Parent as Null as well by using this (Table, Old Parent, '');\n\n  The code in this example shows how to create a custom table without any extension.\n  Then creating a record and then again creating a record and checking in the task table after it gets extended to the task table.\n\n  If the code returns false means the record is not present in the task table as it is not extended. It returns true only when the record is found in the task table after the table is extended.\n\n*/\n\n/* Run the following code to create the custom table without an extension\ncreateCustomTable('u_custom_table','Custom Table'); // Creating a custom Table (don't create if you have one)\nvar CreatedRecord = CreateCustomRecord('u_custom_table'); // Creating a record in the custom table\ngs.print(CheckRecord('u_custom_table'));  // Checking of the record in the task table by passing the Class Name (A.K.A your custom Table)\n\nvar getParentTables = new SNC.CMDBUtil.reParentTable('u_custom_table','','task'); // Extending the custom table to task table . This Works only when the table is extendable\nvar CreatedRecordAfter = CreateCustomRecord('u_custom_table'); // Creating a record in the custom table\ngs.print(CheckRecord('u_custom_table'));  // Checking of the record in the task table by passing the Class Name (A.K.A your custom Table)\n*/\n\n// If the custom Table is already created, run this. (Do not run these 4 lines when creating the table)\ngs.print(CheckRecord('u_custom_table')); //Checking of the record in the task table by passing the Class Name (A.K.A your custom Table)\nvar getParentTables = new SNC.CMDBUtil.reParentTable('u_custom_table','','task'); //Extending the custom table to task table . This Works only when the table is extendable\nvar CreatedRecordAfter = CreateCustomRecord('u_custom_table'); //Creating a record in the custom table\ngs.print(CheckRecord('u_custom_table'));  //Checking of the record in the task table by passing the Class Name (A.K.A your custom Table)\n\n\nfunction createCustomTable(Name, Label)\n{\n  var customTable = new GlideRecord('sys_db_object');\n  customTable.initialize();\n  customTable.name=  Name ;\n  customTable.label= Label ;\n  customTable.is_extendable= true;\n  customTable.insert();\n}\n\nfunction CreateCustomRecord(customTableName)\n{\n  var customTable = new GlideRecord(customTableName);\n  customTable.initialize();\n  return customTable.insert();\n}\n\nfunction CheckRecord(customTableName)\n{\n  var task =new GlideRecord('task');\n  task.addQuery('sys_class_name', customTableName);\n  task.query();\n\n  if(task.next())\n  {\n    return true;\n  }\n\n  return false;\n}\n```\n\nOutput:\n\n```\n*** Script: true\n*** Script: true\n```\n\n"}}