How to get value of field from GlideQuery

Mrman
Tera Guru

Hi All,

Can Someone please help me on this Business rule I am working .I am creating a after business rule on a custom table called u_test_cmdb_ci   and this runs when inserted or updated.

I want a field from another custom table called u_met_integration_maps . Please see the below code and suggest me the changes I need to do . I am getting result as undefined.

Log: METValue undefined

Code:

======

(function executeRule(current, previous /*null when async*/) {

var hierarchypath = current.u_hierarchypath;
var Max = new GlideRecord('u_met_integration_maps');
Max.addQuery('u_module','CMDBCI');
Max.addQuery('u_direction','Inbound [ System > MET ]');
Max.addQuery('u_mapped_sys_value', hierarchypath);
Max.query();
   
var r = Max.u_met_value;
    gs.log("METValue "+r);
  current.u_cmdb_class_name = r;
  current.update();


})(current, previous);

1 ACCEPTED SOLUTION

That indicates something is wrong with the query itself.


Check the field types and values closely to ensure what you are looking for is exactly what you need.



As a side test, can you build a filter on the u_met_integration_maps table to get a record using the same parameters/values?



E.g.


Module | is | CMDBCI     AND


Direction | is | Inbound [ System > MET ]     AND


Mapped sys value | is | (some value you know for hierarchy path)



Does that work from the list? If so, you could copy the query there and use an addEncodedQuery() method.



This video may help:


Video: Scripting Complex Queries Quickly


View solution in original post

23 REPLIES 23

Chuck Tomasi
Tera Patron

You never did a Max.next() to retrieve a record.



(function executeRule(current, previous /*null when async*/) {



var hierarchypath = current.u_hierarchypath;
var Max = new GlideRecord('u_met_integration_maps');
Max.addQuery('u_module','CMDBCI');
Max.addQuery('u_direction','Inbound [ System > MET ]');
Max.addQuery('u_mapped_sys_value', hierarchypath);
Max.query();
   


if (Max.next()) {
    var r = Max.u_met_value;
    gs.log("METValue "+r);
  current.u_cmdb_class_name = r;
  current.update();


}



})(current, previous);


Hi Chuck,



If I am add if then it is not coming to that step and cant even see it is getting logged. Please suggest



(function executeRule(current, previous /*null when async*/) {

var hierarchypath = current.u_hierarchypath;
var Servertype = current.u_ci_server_type;
var Max = new GlideRecord('u_met_integration_maps');
gs.log("Running BR");
Max.addQuery('u_module','CMDBCI');
Max.addQuery('u_direction','Inbound [ System > MET ]');
Max.addQuery('u_mapped_sys_value', hierarchypath);
Max.query();
if(Max.next()){
 
var r = Max.u_met_value;
 
  gs.log("METValue "+r);
  current.u_cmdb_class_name = Max.u_met_value;
  current.update();

}


})(current, previous);


If you're not getting anything, then it likely means there are no records matching your query. You can validate this by adding a gs.log() statement to output how many rows were retrieved.



(function executeRule(current, previous /*null when async*/) {



var hierarchypath = current.u_hierarchypath;
var Servertype = current.u_ci_server_type;
var Max = new GlideRecord('u_met_integration_maps');
gs.log("Running BR");
Max.addQuery('u_module','CMDBCI');
Max.addQuery('u_direction','Inbound [ System > MET ]');
Max.addQuery('u_mapped_sys_value', hierarchypath);
Max.query();



gs.log(Max.getRowCount() + ' rows retrieved');



if(Max.next()){
 
var r = Max.u_met_value;
 
  gs.log("METValue "+r);
  current.u_cmdb_class_name = Max.u_met_value;
  current.update();



}


})(current, previous);


Hi Chuck,



It is returning 0 rows . I can see following in log.



0 rows retrieved . Please suggest