- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2022 05:05 PM
Experts, I would like to create tags on the server table using information from the server table itself and update the key - value pair.
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 below
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 05:25 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2022 10:42 PM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2022 04:17 AM
@Kumar, the application_code does not reside in the server table. Therefore, the gr.application_code will return undefined.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2022 08:53 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 05:25 AM
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();
}