- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2016 02:15 PM
All,
I am having trouble with my script. I need to query the CI relationship table to only pull in the child application environment CI that is related to the Parent Application CI.
Two field on the change form
1. Configuration Item field
2. Environment field //with a reference qual = javascript:u_getmyCIRel()
My client wants to only show the child application environment when the Parent Application CI is selected. I have created a script include that queries the CI relationships table, but my issue is that it is showing all child CI's on the application environment table instead of only showing what is related to the Parent Application CI.
function u_getmyCIRel() {
var rel = [];
var includes = current.cmdb_ci;
var ci = new GlideRecord('cmdb_rel_ci');
ci.addQuery('parent', includes);
ci.addQuery('parent.sys_class_name', "=", 'cmdb_ci_appl');
ci.query();
while (ci.next()) {
var t = new GlideRecord('cmdb_rel_ci');
t.addQuery('child',ci.child);
t.addQuery('child.sys_class_name', "=", "Application Environment");
t.query();
while (t.next()){
if(t.child.sys_class_name == "Application Environment"){
if (rel.length > 0) {
rel += "," + ci.child;
}
else {
rel = '' + ci.child;
}
}
//return 'sys_idIN' + rel;
return rel;
}
}
}
Any help would be great.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2016 09:59 AM
my bad Brett I thought the Environment field was a reference to the cmdb_rel_ci but it is to the Application Environment table am I correct?
if so then the script should read:
function u_getmyCIRel(ci) {
var rel = '';
var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('parent', ci);
gr.addQuery('parent.sys_class_name', "=", 'cmdb_ci_appl');
gr.addQuery('child.sys_class_name', "=", "Application Environment");
gr.query();
gs.log(gr.getRowCount());
while (gr.next()) {
rel += "," + gr.child;
}
gs.log('sys_idIN' + rel.substring(1));
return 'sys_idIN' + rel.substring(1);
}
try running this under a test case scenario then go to the log statements and see what they are printing out or if they are even printing for that matter.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2016 08:09 PM
try this let me know if it helps
Environment field //with a reference qual = javascript:u_getmyCIRel(current.cmdb_ci)
function u_getmyCIRel(ci) {
return 'parent=' + ci+'^child.sys_class_name=Application Environment';
}
if you are looking at doing checks on the CI that is selected to make sure it is a Application you can add on to this script. Also be sure that the class you are getting is not the label but the value of the choice like 'Application Software' is the label, but the value is 'cmdb_ci_app_server' I am not sure what Application Environment's value is.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2016 06:09 AM
Hi Nathan,
Thank you and I tried your suggestion and it is still giving me all Application Environment CI's.
I doubled checked and the sys_class_name value is 'Application Environment'
Not sure what else to do...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2016 08:56 AM
Here are some way to troubleshoot this...
1) add a few gs.log() lines to make sure the script is being triggered and to make sure the output is correct.
2) another easy way to double check the correct query for the ref qual is to go to the cmdb_rel_ci table and do a test case and query the table for the data you want if you were to pick a certain field in the cmdb_ci field. Then just right click the query in the list view and copy it from there you can use this output as a template for the return statement hardcoded as a String literal and make sure that the ref qual is at least working.
let me know if I can help more.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2016 09:59 AM
my bad Brett I thought the Environment field was a reference to the cmdb_rel_ci but it is to the Application Environment table am I correct?
if so then the script should read:
function u_getmyCIRel(ci) {
var rel = '';
var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('parent', ci);
gr.addQuery('parent.sys_class_name', "=", 'cmdb_ci_appl');
gr.addQuery('child.sys_class_name', "=", "Application Environment");
gr.query();
gs.log(gr.getRowCount());
while (gr.next()) {
rel += "," + gr.child;
}
gs.log('sys_idIN' + rel.substring(1));
return 'sys_idIN' + rel.substring(1);
}
try running this under a test case scenario then go to the log statements and see what they are printing out or if they are even printing for that matter.