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

vinothkumar
Tera Guru

Hello Ravi,



Have you manually checked in the table "u_met_integration_maps" returning records by adding filter condition, also please keep in mind you have to pass sys_id, if you are adding reference field in the addQuery condition.



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


Thanks everyone , I was able to figure out as I was not doing correct query.


Glad you got it figured out Ravi. Can you share the specifics of the error and/or correction?


Hi Chuck,



Actually I am getting value set for a single matching query conditions , however I have 3 records having same "hierarchypath" .



this query should be seeing 3 records however when I tested by updating a single record field in this table , it is setting the value only for this record .



Please let me know how do I dynamically set the value for all matching records .


Change the if (Max.next())



to while (Max.next())



Keep in mind, you have a current.update() in that while loop, so it's going to update the current record three times. You may want to save that until after yyou get out of the loop and you've got the values from all three records.