How to create on the server table, server's key - value pair tags using fields from the server table form using a script.

Indigo
Mega Contributor

Experts, I would like to create tags on the server table using information from the server table itself and update the key - value pair. find_real_file.png

Essentially, take the Environment value, Hardware Status value and Application Code value and create a key - value pair tag. Example env - prod, hardware_status - installed, application_code - xxx and insert them in the cmdb_key_value table. As shown belowfind_real_file.png

At the moment, am working on updating the records present. I think in the future, I'll need to insert this info on insertion of the record. Anyways here is my background script at the moment and the logic

1.Get to the server table and filter the needed servers - those that do have an app_code, hardware_status = installed,
and env is prod < for now>

var gr = new GlideRecord('cmdb_ci_server');
gr.addEncodedQuery('u_server_app_name.u_application_codeISNOTEMPTY^hardware_status=installed^u_environment=Production');
gr.query();
while (gr.next()) {
gs.print(gr.name + ":" + gr.u_environment + ":" + gr.hardware_status);

}
gs.print(gr.getRowCount());

2. Get the Application Code for each server and print it

// HAVING MAJOR TROUBLES IMPLEMENTING THIS


3.Take the hardware status values, application code and environment and insert them in the cmbd_key_value table

 

1 ACCEPTED SOLUTION

cmdb_ci_win_server.u_server_app_name.u_application_code. Dot - walking had to be implemented.

var limit = 10;
var total = 0;
var arr=[];
var gr = new GlideRecord('cmdb_ci_server');
gr.addEncodedQuery('u_server_app_name.u_application_codeISNOTEMPTY^hardware_status=installed^u_environment=Production');
gr.query();
while (gr.next() && total < limit) 
{
	// Get the values needed for the tags we care about.
	var environment = gr.getDisplayValue('u_environment');
	var hardwareStatus = gr.getDisplayValue('hardware_status');
	var applicationCode = gr.u_server_app_name.u_application_code + '';
	var sysId = gr.sys_id + '';
	
	gs.print(gr.u_server_app_name.u_application_code);
	
	arr.push({'env' : environment,'hardware_status' : hardwareStatus, 'code' : applicationCode, 'sys_id' : sysId}); //Set values as per the key-value format

gs.print('Server is: ' + gr.name);
	
	++total;
}

insertRecInCmdbKeyValueTable(arr); //Function to insert record in cmdb_key_value table

function insertRecInCmdbKeyValueTable(arrData) 
{
	var insertRecord = new GlideRecord('cmdb_key_value');
	for(var i=0; i<arrData.length; i++) 
	{ 
		//Loop over array of object to fetch values
		insertTag('env', arrData[i].env, arrData[i].sys_id);
		insertTag('hardware_status', arrData[i].hardware_status, arrData[i].sys_id);
		insertTag('app', arrData[i].code, arrData[i].sys_id);
	}
}

function insertTag (key, value, sysId)
{
	// Delete the old tag value
	var entry = new GlideRecord('cmdb_key_value');
	entry.addEncodedQuery('configuration_item=' + sysId + '^key=' + key);
	entry.query();
	// be safe
	if (entry.getRowCount() == 1)
	{
		entry.deleteMultiple();	
	}	

	// Initialize a new record and insert it.
	entry = new GlideRecord('cmdb_key_value');
	entry.setValue('key', key);	
	entry.setValue('value', value);	
	entry.setValue('configuration_item', sysId);
	entry.insert();
}

 

View solution in original post

4 REPLIES 4

Sai Kumar B
Mega Sage
Mega Sage

@Indigo Try the below code

var arr=[],
var gr = new GlideRecord('cmdb_ci_server');
gr.addEncodedQuery('u_server_app_name.u_application_codeISNOTEMPTY^hardware_status=installed^u_environment=Production');
gr.query();
while (gr.next()) {
arr.push({'env' : gr.u_environment,'hardware_status' : gr.hardware_status, 'code' : gr.application_code}); //Set values as per the key-value format
}

insertRecInCmdbKeyValueTable(arr); //Function to insert record in cmdb_key_value table

function insertRecInCmdbKeyValueTable(arrData) {
var insertRecord = new GlideRecord('cmdb_key_value');
for(var i=0; i<arrData.length; i++) { //Loop over array of object to fetch values
insertRecord.intialize();
insertRecord.setValue('field_1', arrData[i].env);
insertRecord.setValue('field_2', arrData[i].hardware_status);
insertRecord.setValue('field_3', arrData[i].code);
insertRecord.insert();
}

@Kumar, the application_code does not reside in the server table. Therefore, the gr.application_code will return undefined. 

@Indigo May I know where it resides? 

cmdb_ci_win_server.u_server_app_name.u_application_code. Dot - walking had to be implemented.

var limit = 10;
var total = 0;
var arr=[];
var gr = new GlideRecord('cmdb_ci_server');
gr.addEncodedQuery('u_server_app_name.u_application_codeISNOTEMPTY^hardware_status=installed^u_environment=Production');
gr.query();
while (gr.next() && total < limit) 
{
	// Get the values needed for the tags we care about.
	var environment = gr.getDisplayValue('u_environment');
	var hardwareStatus = gr.getDisplayValue('hardware_status');
	var applicationCode = gr.u_server_app_name.u_application_code + '';
	var sysId = gr.sys_id + '';
	
	gs.print(gr.u_server_app_name.u_application_code);
	
	arr.push({'env' : environment,'hardware_status' : hardwareStatus, 'code' : applicationCode, 'sys_id' : sysId}); //Set values as per the key-value format

gs.print('Server is: ' + gr.name);
	
	++total;
}

insertRecInCmdbKeyValueTable(arr); //Function to insert record in cmdb_key_value table

function insertRecInCmdbKeyValueTable(arrData) 
{
	var insertRecord = new GlideRecord('cmdb_key_value');
	for(var i=0; i<arrData.length; i++) 
	{ 
		//Loop over array of object to fetch values
		insertTag('env', arrData[i].env, arrData[i].sys_id);
		insertTag('hardware_status', arrData[i].hardware_status, arrData[i].sys_id);
		insertTag('app', arrData[i].code, arrData[i].sys_id);
	}
}

function insertTag (key, value, sysId)
{
	// Delete the old tag value
	var entry = new GlideRecord('cmdb_key_value');
	entry.addEncodedQuery('configuration_item=' + sysId + '^key=' + key);
	entry.query();
	// be safe
	if (entry.getRowCount() == 1)
	{
		entry.deleteMultiple();	
	}	

	// Initialize a new record and insert it.
	entry = new GlideRecord('cmdb_key_value');
	entry.setValue('key', key);	
	entry.setValue('value', value);	
	entry.setValue('configuration_item', sysId);
	entry.insert();
}