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

harshinielath
Tera Expert

Hi


Is the u_business_service_ci field have multiple values? And bsci.get('sys_id of record'),so make sure u r passing sys_id and not the string(current.u_business_service_ci.toString())


And usually it is not recommended to do glide record querying inside loop,so the best practice is to put the desired records in array and then query.


Shishir Srivast
Mega Sage

Can you please give a try with,



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.query();  


while(rel.next()){


var srv = new GlideRecord('cmdb_ci_server');  


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


if (srv.u_sox_report_related)  


return true;  


}


}  


return result;


}  


}  


}  





Shishir - That didn't give the right results, returned with a false when it should have been a true.


Can we change this line and then try.



if (srv.u_sox_report_related)



to



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