VaranAwesomenow
Mega Sage
Options
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
‎07-30-2024
11:13 PM
Requirement : Create Dyanmic CI group for a list of dynamic CI group names.
Background : There are close to 70 + dynamic CI groups which need to be created based on a custom attribute in server table called application name.
Solution: In order to create a dynamic CI group correctly we need to create records in three tables
cmdb_ci_query_based_service -> Dynamic CI Group
cmdb_group -> CMDB Group
cmdb_group_contains_encoded_query -> Encoded query
var sDebug = 'Debug from dynamic ci group creation script' + "\n";
var dcGList;
dcGList = 'dynamic ci group g1|dynamic ci group g2';
var dcGArray = [];
dcGArray = dcGList.split("|");
sDebug += 'Length of array=' + dcGArray.length + "\n";
var dcGSysId, cgSysId, enQuerySysId;
var queryClass = 'cmdb_ci_server';
for (var k = 0; k < dcGArray.length; k++) {
var dcName = dcGArray[k].trim();
sDebug += 'Working on name=' + dcName + "\n";
cgSysId = checkCMDBGroup(dcName); //always check if cmdb group is present.
sDebug += 'CMDB group returned =' + cgSysId + "\n";
enQuerySysId = checkEnQuery(cgSysId, dcName); // There is no name field in encoded query table.
sDebug += 'Encoded query returned =' + enQuerySysId + "\n";
if (JSUtil.notNil(cgSysId) && JSUtil.notNil(enQuerySysId)) {
dcGSysId = checkDCGroup(dcName, cgSysId); //Check if dynamic CI group is present if not create one and associate to cmdb group.
sDebug += 'Dynamic CI group returned =' + dcGSysId + "\n";
if (JSUtil.notNil(dcGSysId)) {
sDebug += 'Dynamic CI group is present for ' + dcName + "\n";
} else {
sDebug += 'Failed to create dynamic ci group but it has cmdb group with sysId' + cgSysId + "\n";
}
} else {
sDebug += 'Failed to create dynamic ci group, there is no cmdb group or endcoded query for ' + dcName + "\n";
}
}
gs.print(sDebug);
function checkCMDBGroup(dcgName) {
sDebug += 'Querying CMDB query' + "\n";
var grcmdbGroup = new GlideRecord('cmdb_group');
var grcmdbSysId;
var dcGroupEnQuery = 'group_name=' + dcgName;
grcmdbGroup.addEncodedQuery(dcGroupEnQuery);
grcmdbGroup.query();
if (grcmdbGroup.next()) {
grcmdbSysId = grcmdbGroup.sys_id.toString();
sDebug += 'CMDB query is present' + "\n";
} else {
grcmdbGroup.initialize();
grcmdbGroup.group_name = dcgName;
grcmdbSysId = grcmdbGroup.insert();
sDebug += 'CMDB query is created' + "\n";
}
return grcmdbSysId;
}
function checkEnQuery(cmdbGroupSysId, dcgName, queryClass) {
sDebug += 'Querying encoded query' + "\n";
var grEnQuery = new GlideRecord('cmdb_group_contains_encoded_query');
var enCondition = 'operational_status=1^ORinstall_status=1^attributesLIKE' + dcgName + "^EQ";
var grEnSysId;
var dcGroupEnQuery = 'group=' + cmdbGroupSysId;
grEnQuery.addEncodedQuery(dcGroupEnQuery);
grEnQuery.query();
if (grEnQuery.next()) {
sDebug += 'Encoded query is present' + "\n";
grEnSysId = grEnQuery.sys_id.toString();
} else {
grEnQuery.initialize();
grEnQuery.group = cmdbGroupSysId;
grEnQuery.condition = enCondition;
grEnQuery.setValue('class','cmdb_ci_server');
grEnSysId = grEnQuery.insert();
sDebug += 'Creating Encoded query' + "\n";
}
return grEnSysId;
}
function checkDCGroup(dcgName, cmdbGroup) {
sDebug += 'Querying CMDB Group' + "\n";
var grdcGroup = new GlideRecord('cmdb_ci_query_based_service');
var grdcSysId;
var dcGroupEnQuery = 'name=' + dcgName;
grdcGroup.addEncodedQuery(dcGroupEnQuery);
grdcGroup.query();
if (grdcGroup.next()) {
sDebug += 'CMDB group is present' + "\n";
grdcSysId = grdcGroup.sys_id.toString();
} else {
grdcGroup.initialize();
grdcGroup.name = dcgName;
grdcGroup.cmdb_group = cmdbGroup;
grdcSysId = grdcGroup.insert();
sDebug += 'Creating CMDB group' + "\n";
}
return grdcSysId;
}
Script explanation (generated using IBM Granite and Meta.ai) :
This JavaScript code is designed to create dynamic CI (Configuration Item) groups in ServiceNow, a cloud-based IT service management platform. Here's a breakdown of the code:
Main Script
-
The script starts by defining a debug variable
sDebug
to track the execution process. -
It then defines a list of dynamic CI group names (
dcGList
) and splits them into an array (dcGArray
). -
The script loops through each group name in the array and:
-
Checks if a CMDB group exists for the current name using the
checkCMDBGroup
function. -
If a CMDB group exists, it checks if an encoded query exists for the group using the
checkEnQuery
function. -
If both CMDB group and encoded query exist, it checks if a dynamic CI group exists using the
checkDCGroup
function. If not, it creates one.
Functions
-
checkCMDBGroup(dcgName)
: -
Queries the
cmdb_group
table for a group with the given name. -
If found, returns the sys_id of the existing group.
-
If not found, creates a new CMDB group with the given name and returns its sys_id.
-
checkEnQuery(cmdbGroupSysId, dcgName)
: -
Queries the
cmdb_group_contains_encoded_query
table for an encoded query associated with the given CMDB group sys_id and name. -
If found, returns the sys_id of the existing encoded query.
-
If not found, creates a new encoded query with the given condition and returns its sys_id.
-
checkDCGroup(dcgName, cmdbGroup)
: -
Queries the
cmdb_ci_query_based_service
table for a dynamic CI group with the given name. -
If found, returns the sys_id of the existing group.
-
If not found, creates a new dynamic CI group with the given name and associates it with the given CMDB group sys_id.
Debugging
The script uses the
sDebug
variable to track the execution process, appending messages at each stage. Finally, it prints the entire debug log using gs.print(sDebug)
.In summary, this script automates the creation of dynamic CI groups in ServiceNow by checking for existing CMDB groups, encoded queries, and dynamic CI groups, and creating them if necessary.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.