Scripting to find values through CDMB Relationships on a Workflow

Jon S_
Giga Contributor

So, I've tried to piece together scripts that I've used before for this but could really use some help.

On a Change [change_request], there is a field [u_business_service_ci] that references one or many CMDB Business Services [cmdb_ci_service]. A Business Service is then related to one or more Servers [cmdb_ci_server] through the CI Relationship table [cmdb_rel_ci] (Parent is [cmdb_ci_service], Type is [Owns::Owned by], Child is [cmdb_ci_server]). On the referenced Servers, there is a True/False field called SOX Report Related [u_sox_report_related]. When a Change is submitted, on the workflow I need an If activity script that checks through all of the Change --> Business Services --> Servers to see if that checkbox is present for at least one and return true.

This is what I've came up with but to no surprise it isn't working. Any ideas?

answer = ifScript();

function ifScript() {

        var result = false;

        var list = current.u_business_service_ci.toString();

        var listArr = list.split(',');

        for (var key in listArr) {

                  var bsci = new GlideRecord('cmdb_ci_service');

                  if (bsci.get(listArr[key])) {

                            var rel = new GlideRecord('cmdb_rel_ci');

                            rel.addQuery('parent', bsci.getUniqueValue());

                            rel.addQuery('type.name', "Owns::Owned by");

                            rel.addQuery('parent.sys_class_name', "cmdb_ci_service");

                            rel.query();

                            var ciIds = [];

                            if (rel.get(listArr[ciIds])) {

                                      var srv = new GlideRecord('cmdb_ci_server');

                                      if (srv.u_sox_report_related) {

                                                return true;

                                      }

                            }

                            return result;

                  }

        }

}

1 ACCEPTED SOLUTION

I think, I got this, we are returning the result within for loop that's why, i believe return will break the for loop, can we put the return result outside of for loop and try.



answer = ifScript();



function ifScript() {


        var result = false;


        var list = current.u_business_service_ci.toString();


      gs.log('****** Business Services List **** :   ' + list);


        var listArr = list.split(',');


        for (var key=0; key < listArr.length; key++) {


                  var bsci = new GlideRecord('cmdb_ci_service');


                  if (bsci.get(listArr[key])) {


                            var rel = new GlideRecord('cmdb_rel_ci');


                            rel.addQuery('parent', bsci.getUniqueValue());


                            rel.addQuery('type.name', "Owns::Owned by");


                            rel.query();


                            while(rel.next()){


                                      var srv = new GlideRecord('cmdb_ci_win_server');


                                      if(srv.get('sys_id', rel.child)){


                                                if (srv.u_sox_report_related)


                                                          return true;


                                      }


                            }                            


                  }


        }


return result;


}


View solution in original post

25 REPLIES 25

I Apologies, i missed to add the highlighted thing in log statement. Can you please check again.



function ifScript() {  


var result = false;  


var list = current.u_business_service_ci.toString();  


var listArr = list.split(',');  


for (var key in listArr) {  


var rel = new GlideRecord('cmdb_rel_ci');  


rel.addQuery('parent', listArr[key]);  


rel.addQuery('type.name', "Owns::Owned by");  


rel.query();  


while(rel.next()){


var srv = new GlideRecord('cmdb_ci_server');  


if(srv.get('sys_id', rel.child)){


if (srv.u_sox_report_related == 'true' || srv.u_sox_report_related == 'false') {


gs.log('****u_sox_report_related   in IF****     : ' + srv.u_sox_report_related );


return true;


}


else{


gs.log('****u_sox_report_related   in ELSE****     :' + srv.u_sox_report_related );


}


}


}  


return result;  


}  


}  


Sorry, that was the issue but I should have made that transparent.



I updated with your script though and still getting the undefined:


****u_sox_report_related in ELSE**** :undefined


Is the record we are checking has u_sox_report_related field? just want to cross check. and also let's update the log with below line and see if that logs anything other than .undefined?


gs.log('****u_sox_report_related   in ELSE****     :' + srv.u_sox_report_related.toString());



Yes, I've quadruple checked that over and over. That change is still getting us: ****u_sox_report_related in ELSE**** :undefined



I had already created a System Definition-->Relationship to show the related Server on the Business Service record via the related list and it is working. This is the query, if that helps:


(function refineQuery(current, parent) {


var rel = new GlideRecord('cmdb_rel_ci');


rel.addQuery('parent', parent.getUniqueValue());


rel.addQuery('type.name', "Owns::Owned by");


rel.addQuery('parent.sys_class_name', "cmdb_ci_service");


rel.query();


var ciIds = [];


while (rel.next())


ciIds.push(rel.getValue('child'));


current.addQuery('sys_id', 'IN', ciIds);


})(current, parent);


I think. related list data are fine as long as we are directly querying the table, I was trying on my instance (in background script) with two records in cmdb_rel_ci table and it works fine with below example.



var rel = new GlideRecord('cmdb_rel_ci');      


rel.addQuery('parent', '27e3a47cc0a8000b001d28ab291fa65b');      


rel.addQuery('type.name', "Runs on::Runs");      


rel.query();      


while(rel.next()){  


var srv = new GlideRecord('cmdb_ci_server');      


if(srv.get('sys_id', rel.child)){


if (srv.u_sox_report_related == true || srv.u_sox_report_related == false)


gs.print('***********Found u_sox_report_related RECORDS************' + srv.u_sox_report_related);


else


gs.print('***********No u_sox_report_related RECORDS************' + srv.u_sox_report_related);


}              


}      



output:


*** Script: ***********Found u_sox_report_related RECORDS************true


*** Script: ***********Found u_sox_report_related RECORDS************false



Cam you please replace the highlighted text accordingly and try in background script.