- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2015 02:43 PM
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?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2015 05:46 PM
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):
Note that this exact script won't work in a scoped app, because getGlideObject isn't available there.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2015 05:46 PM
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):
Note that this exact script won't work in a scoped app, because getGlideObject isn't available there.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2015 10:21 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-28-2021 10:38 AM
This Business rule should be on which table if you want to see the variable labels in the catalog task description?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2016 07:13 AM
Almost a year later and this has solved my problem. Thanks!