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

HI Ravi


can u pls try tihs



(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(Max.getRowCount()');
  current.u_cmdb_class_name = Max.u_met_value;
  current.update();


}


})(current, previous);


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


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Ravi



Can you see in the log the message Running BR ?


Because if not probably the condition of the BR is not met and the BR not executed.



I would also check the values for the inputs of your query



Cheers


R0b0


DUGGI
Giga Guru

Add if condition after .query();



if(Max.next()){




var r = Max.u_met_value;


    gs.log("METValue "+r);


  current.u_cmdb_class_name = r;


  current.update();


}


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Ravi



First of all I would suggest to change the script to



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()){


       


      gs.log(Max.sys_id);


      var r = Max.u_met_value;


      gs.log("METValue "+r);


      current.u_cmdb_class_name = r;


      current.update();


}



Check what it is the sys_id returned.



Cheers


R0b0