Help with CI Relationship Script

Brett14
Giga Expert

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.

1 ACCEPTED SOLUTION

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.


View solution in original post

7 REPLIES 7

I just tried this in my instance and it worked let me know if you need any help


Nate,



My apologies, I am just getting to my emails.   You are correct the Environment field is referencing the 'Application Environment' table.   Awesome, let me find some time to test this out and I will get back with you.



Again thank you for your time on this.  


Hi Nate,



The script worked and the client confirmed through testing.   Requirement is finally closed.   Also, I discovered below the actual missing link through your help.



I doubled checked the ref qual on the CI relationships table and what I discovers is that 'Application Environment' is acutally 'u_cmdb_ci_app_environment'. However, when looking at the choice list for Class field the value is 'Application Environment' instead of 'u_cmdb_ci_app_environment'.   Right now it doesn't make sense, but you helped me dig deeper.



I switched out       gr.addQuery('child.sys_class_name', "=", "Application Environment");   for   gr.addQuery('child.sys_class_name', "=", "u_cmdb_ci_app_environment"); and everything worked as it should.  



Again thank you for your help on this and Im marking your answer correct.