I’m developing a new ServiceNow catalog item that takes an input of a Linux Server name and has a requirement of locating the associated vCenter reference for it and adding it to a field on the catalog item to make it available for a back end flow. I can find the vCenter reference manually with query builder, connecting the cmdb_ci_linux_server & cmdb_ci_esx_server tables (cmdb classes), but haven’t had success yet working with the Catalog Client script GlideRecord approach. Any thoughts, ideas, or examples on how to do this?
1)//Testing in Background Scripts, I can find the valid sys_id for a known linux server
var test1svr = new GlideRecord('cmdb_ci_linux_server');
test1svr.addQuery('name', 'example_server_name');
test1svr.query();
if (test1svr.next()) {
//found, nothing further needed in this leg
}
//return test1svr.sys_id
gs.info(test1svr.sys_id)
2)//Testing in Background Scripts, interim step of locating the associated ESX server for a given linux
//server ... with the Virtualized by::Virtualizes relationship type ... querying the Client
//Relationship table
var rel = new GlideRecord('cmdb_rel_ci');
rel.addQuery('parent', 'example linux server sys id');
rel.addQuery('type', 'example sys id for Virtualized by::Virtualizes type');
rel.query();
if (rel.next()) {
//found, nothing further needed in this leg
}
//return rel.sys_id
//gs.info(rel.sys_id)
gs.info(rel.parent)
gs.info(rel.child)
gs.info(rel.type)
//gs.info(rel.sys_id.name)
gs.info(rel.parent.name)
gs.info(rel.child.name)
gs.info(rel.type.name)
Successful results ... I verified the sys_id's & names were the ones I was expecting. The parent in this case is the linux server and the child is the associated ESX server for the Virtualized by::Virtualizes relationship type.
3) I tried the interim step of finding the associated ESX server using an onChange Catalog Client script, but this doesn't seem to be the right approach, since it's ignoring me ... I keep getting my "testing outside the if" . My interim goal is to find the associated ESX Server, populate a variable with the value and use that to find the vCenter Reference on the configuration tab of the associated ESX Server.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Locating the associated ESX server
// var rel1 = new GlideRecord('cmdb_rel_ci');
// rel1.addQuery('parent', u_server);
// rel1.addQuery('type', 'example sys id for type');
// rel1.query();
// if (rel1.next()) {
//found, nothing further needed in this leg
// g_form.setValue("u_associated_esx_server", "testing ...got a hit");
// }
// var rel = new GlideRecord('cmdb_rel_ci');
// var test1 = 'example sys id for linux server';
// rel.addQuery('parent', test1);
// rel.addQuery('type', 'example sys id for type');
// rel.query();
// if (rel.next()) {
// //found, nothing further needed in this leg
// g_form.setValue("u_associated_esx_server", "testing ... inside if");
// }
g_form.setValue("u_associated_esx_server", "testing ... outside if");
}