GlideRecord getValue() does not allow a dot-walked value but addQuery() does.

ty_roach
Tera Guru

I was hoping to use the GlideRecord function "getValue()" to retrieve the value of a dot-walked field but it appears I cannot. The "addQuery()" function allows you to specify a dot-walked field so I was hoping the same would be true for getValue().

 

Is this intentional and if so, why?

 

Here's the code example to demonstrate the point. I've included the script include TyTest that has two functions, test1() which uses the getValue() and does not work and test2() which does an addQuery() and does work.

var TyTest = Class.create();
TyTest.prototype = {
   initialize: function() {
   },

   test1: function () {
   var gr = new GlideRecord('pm_portfolio_risk');
   var group = new GlideRecord('sys_user_group');

   if (group.get(gr.getValue('pm_portfolio_project.assignment_group'))) {
   gs.log('successfully retrieved group from a dot-walked getValue() call.');
   }
   else {
   gs.log('failed to retrieved group from a dot-walked getValue() call.');
   }
   },

   test2: function () {
   // find a project risk whose project assignment group is Chat Support
   var gr = new GlideRecord('pm_portfolio_risk');
   gr.addQuery('pm_portfolio_project.assignment_group','e681ea120a0007dc5aba18ffb6b3e829'); // Chat Support
   gr.query();
   if (gr.next()) {
   gs.log('successfully found a group by dot-walking a value in an addQuery() call.');
   }
   else {
   gs.log('failed to find a group by dot-walking a value in an addQuery() call.');
   }
   },

   type: 'TyTest'
};

results from running new TyTest().test1() --

*** Script: failed to retrieved group from a dot-walked getValue() call.

 

results from running new TyTest().test2() --

*** Script: successfully found a group by dot-walking a value in an addQuery() call.

 

The reason I need to use getValue() is because, unlike my TyTest example, I do not know the field I am trying to get the group value from at coding time...it's dynamic and will be determined at run-time. So, at run time, I need to look into a table to figure out what field has the field that tells me which assignment group to look for and in this case, the value returned at run-time is a dot-walked value (i.e. 'pm_portfolio_project.assignment_group' --- from the pm_portfolio_risk table). So, what I was trying to do was something like this:

 

if (group.get(gr.getValue(field_having_assignment_group))) {

// do work on the group

}

else {

// could not find the group.

}

 

I used my TyTest() script include to demonstrate this problem. Does anyone have a suggestion as to how to best solve this?


Here's a snippet of the actual code to help put it all in context for you:

   var group = new GlideRecord('sys_user_group');
   var map = new GlideRecord('u_map');
   map.addEncodedQuery('sys_updated_onONToday@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)');
   map.query();
   while (map.next()) {

   if (map.u_assignment_group == 'none') {
   gs.log(map.u_table + '.'+map.u_assignment_group+' is not a valid assignment_group field...skipping');
   continue;
   }

   var gr = new GlideRecord(map.u_table); // u_table says what table we need to get.
   gr.query();

   while (gr.next()) {
 // map.u_assignment_group will have what field within u_table will have the "assignment_group" value to use for determining the group
   if (group.get(gr.getValue(map.u_assignment_group))) {

1 ACCEPTED SOLUTION

ty_roach
Tera Guru

I've just discovered the answer to this problem: use getElement() and getElementValue().   The getElementValue() DOES allow you to specify a dot-walk string value.


View solution in original post

14 REPLIES 14

Mussie
ServiceNow Employee
ServiceNow Employee

Hi Ty,


Could you please check the below and see if you help?


Dot-Walking within GlideRecord


Thanks,


Mussie


gr.getElement(variable_with_dot_walk) worked for me.

10 years later and this just helped me out, nice one!

Daniel Oderbolz
Kilo Sage

Actually, the point of getValue is to convert the object reference to a value. To be able to dt-walt, you need an object.


I have just completed a write up of this topic here:



Swissbytes: Service Now Script Includes: Make sure to convert object references to values



Best Daniel



If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel

phillipmelmoth
Mega Expert

Anyone searching for the answer to this, note that using getElement() is not equivalent to getValue() as it returns an object, whereas one of the reasons you'd want to use getValue() is to return the correct variable type, eg: string

 

In this case you can simply use .getRefRecord() before the .getValue(), like this:

gr.parent_gr.grandparent_gr.getRefRecord().getValue('field');