Client Script: Get variable label and values on record

kungfuu72
Giga Expert

I am looking at trying to obtain a variables label and value through a client script preferably using a for loop to go through these variables as variables for each Requested Item may not be the same.


I know that you can get the variable label and value on server side like this example:

for (key in gr.variables) {

  var v = gr.variables[key];

  if(v.getGlideObject().getQuestion().getLabel() != ) {

  gs.log('         ' +   v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");  

  }

}

On the client side, you are also able to get variable values like:

g_form.getValue('variables.cpu_speed');

But there does not seem to be any direct way of doing this cleanly through the client side.

A few thoughts I had was doing an AJAX call, but not really preferred.

The other thought would be crazy jQuery to get into the page code, but from my experience, this would be hard to maintain if ServiceNow changes in   future patches and usually a mess to clean up.

Any other thoughts on how to do this cleanly client-side?

1 ACCEPTED SOLUTION

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Daniel,



Doing this client-side is dirty, since g_form doesn't expose any good methods for that. However- you already have tools you need. Do this server-side, in a onDisplay Business Rule, using the code you already have. Put the result (as a JSON string which can be turned back into an object client-side) onto the scratchpad.



Something like this:



(function executeRule(current, previous /*undefined when async or display*/) {


      var obj = {};


      var json = new global.JSON();


   


      for (key in current.variables) {


              var v = current.variables[key];


              if(v.getGlideObject().getQuestion().getLabel() != '') {


                      obj[key] = {


                              label : v.getGlideObject().getQuestion().getLabel(),


                              value : v.getDisplayValue()


                      }


              }


      }


      g_scratchpad.itemVars = json.encode(obj);



})(current, typeof previous != 'undefined' ?   previous : null);



Which yields this client-side (notice that I parse the string back into a JS object):


Screen Shot 2015-08-10 at 5.43.46 PM.png



Note that this exact script won't work in a scoped app, because getGlideObject isn't available there.


View solution in original post

4 REPLIES 4

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Daniel,



Doing this client-side is dirty, since g_form doesn't expose any good methods for that. However- you already have tools you need. Do this server-side, in a onDisplay Business Rule, using the code you already have. Put the result (as a JSON string which can be turned back into an object client-side) onto the scratchpad.



Something like this:



(function executeRule(current, previous /*undefined when async or display*/) {


      var obj = {};


      var json = new global.JSON();


   


      for (key in current.variables) {


              var v = current.variables[key];


              if(v.getGlideObject().getQuestion().getLabel() != '') {


                      obj[key] = {


                              label : v.getGlideObject().getQuestion().getLabel(),


                              value : v.getDisplayValue()


                      }


              }


      }


      g_scratchpad.itemVars = json.encode(obj);



})(current, typeof previous != 'undefined' ?   previous : null);



Which yields this client-side (notice that I parse the string back into a JS object):


Screen Shot 2015-08-10 at 5.43.46 PM.png



Note that this exact script won't work in a scoped app, because getGlideObject isn't available there.


Okay,



I think I will try the Ajax call first, as I'm trying to do this in a UI Action that isn't trivial to begin with that users will use in order to perform the desired action. Rather than have the code run each time given the display business rule (of course with conditions), I think an on-demand call might yield slightly better overall performance.



I'll follow-up when I've completed this and share how it goes.


This Business rule should be on which table if you want to see the variable labels in the catalog task description?

Ken83
Mega Guru

Almost a year later and this has solved my problem. Thanks!