Accessing Catalog Item Variables in an onChange script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2009 07:09 PM
Hi all,
I have a Catalog Item containing a Drop Down List on a Catalog Variable. I have created a Catalog Client Script for the Variable Set/Variable that reacts to a change.
I'd like to do the same thing AFTER the user has ordered the Catalog Item.
I've found the Client Scripts and can get onChange to work for columns on sc_req_item, but cannot work out how to get onChange to work for the Catalog Variable that I have defined.
Has anyone worked out how to do this?
Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2010 07:53 AM
I'm looking for an answer to this issue as well. Did you ever find a solution?
-Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2010 06:28 PM
Hi Chris,
The solution was a HACK.
In an onload script for sc_req_item, I added an onchange function programatically:
g_form.getControl(varName).onchange = function() { ... };
The complication is that varName is not so easy to find. In the page at create time, this works:
g_form.getControl("firstName")
However, once you have created the sc_req_item row and are returning to edit the row, that no longer returns the control. I did some snooping and found that all the html controls in the page have a cryptic id. At create time, you can use the variable name and getControl translates that to the real (cryptic) id and so finds the control for you.
At edit time, for whatever reason, it doesn't translate, so you need to use the cryptic id as the varName. I wrote a function for myself to find the id:
function getSNVariableId(vName) {
// vName is the variable name, e.g. "firstName"
try {
// Can't remember why I did this, but it works
if (!g_sc_form) {
return null;
}
} catch (err) {
return vName;
}
if (!g_sc_form.nameMap) {
return null;
}
if (g_form.getControl(vName)) {
// OK, the getControl function does return something sensible
return vName;
}
// Got here, so we still haven't found the control, so read through nameMap array:
// "prettyName" - e.g. "firstName"
// "realName" - e.g. "ni.VE9cd5b2070a0a780a01879f17455924ac"
for (var i = 0; i < g_sc_form.nameMap.length; i++) {
if (g_sc_form.nameMap.prettyName == vName) {
return g_sc_form.nameMap.realName;
}
}
return null;
}
I think that some of the other g_form API functions/methods need the real Id at edit time.
Why is this so? My guess is that SN only envisaged the catalogue as a "one-shot" order, i.e. customer chooses the features that he/she wants, clicks order and that is it. Here, we have complicated things by wanting to revisit and revise the ordered item.
I did it because I had to (deadlines, politics), but in retrospect I regret it. My scripts are now very closely tied to the internal workings of the UI so there is a risk that they'll break if SN changes stuff there. Anyway, the Service Now product is always evolving, so hopefully they'll extend the API to address this issue, but as you're still looking for a solution, it doesn't look like it yet.
Hope that helps...
...b

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2010 07:55 AM
I'm looking for an answer to this issue as well. Did you ever find a solution?
-Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2010 07:51 PM
Hi Chris, yes, see original post.