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

Shishir - Ok, so this worked, which helped me find two issues that were the cause of the false from your original script way above that I still need some help with:


1. It seems only to be cycling through the first Business Service CI's records. If the Business Service with the related Server that has the checkbox checked is not first in line, it is reporting out false. If that Business Service is first, it works. Any idea why it's not cycling through all?


2. It doesn't seem to cycle through the parent CMDB of a record, such as cmdb_ci_server. The Server I was testing is on the cmdb_ci_win_server, the extended record of cmdb_ci_server. But my Relationship query above works for the same record and will cycle through and finds the Server that is on cmdb_ci_win_server when I use cmdb_ci_server. Any idea why this script is so specific but the other works for all records?


1. It seems only to be cycling through the first Business Service CI's records. If the Business Service with the related Server that has the checkbox checked is not first in line, it is reporting out false. If that Business Service is first, it works. Any idea why it's not cycling through all?



I believe it's because of for loop, can we replace the for loop statement as below and try,


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




2. It doesn't seem to cycle through the parent CMDB of a record, such as cmdb_ci_server. The Server I was testing is on the cmdb_ci_win_server, the extended record of cmdb_ci_server. But my Relationship query above works for the same record and will cycle through and finds the Server that is on cmdb_ci_win_server when I use cmdb_ci_server. Any idea why this script is so specific but the other works for all records?



I think, it's because of cmdb_ci_server is parent of cmdb_ci_win_server class, but it should process, when i click on the record from cmdb_ci_server (or copy the sys_id any individual record it's as same as the sys_id of record a from cmdb_ci_win_server server). if that doesn't help may we can try querying cmdb_ci_win_server table to test initially.


Shishir - With that updated, it's still only going through the first u_business_service_ci in the list and not the second one. And yes, if I change it to cmdb_ci_win_server it does work but I do need this one to find all Servers though as not all of them are Windows.



Here is the updated script:


answer = ifScript();



function ifScript() {


        var result = false;


        var list = current.u_business_service_ci.toString();


        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;


                  }


        }


}


Is u_business_service_ci a list field? Can we put a log as below and see what value are we getting?



  1. answer = ifScript();  
  2.  
  3. function ifScript() {  
  4.         var result = false;  
  5.         var list = current.u_business_service_ci.toString();
  6.       gs.log('****** Business Services List **** :   ' + list);
  7.         var listArr = list.split(',');  
  8.         for (var key=0; key < listArr.length; key++) {  
  9.                   var bsci = new GlideRecord('cmdb_ci_service');  
  10.                   if (bsci.get(listArr[key])) {  
  11.                             var rel = new GlideRecord('cmdb_rel_ci');  
  12.                             rel.addQuery('parent', bsci.getUniqueValue());  
  13.                             rel.addQuery('type.name', "Owns::Owned by");  
  14.                             rel.query();  
  15.                             while(rel.next()){  
  16.                                       var srv = new GlideRecord('cmdb_ci_win_server');  
  17.                                       if(srv.get('sys_id', rel.child)){  
  18.                                                 if (srv.u_sox_report_related)  
  19.                                                           return true;  
  20.                                       }  
  21.                             }  
  22.                             return result;  
  23.                   }  
  24.         }  
  25. }  

Yes, it is a List field.


****** Business Services List **** : 88cf309c0f782e00617d355be1050e4d,fea56b5cdb20aa00af8effefbf961985