- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2015 02:31 PM
Hi there,
In a catalog item, I have a variable reference field called requested_for. It is auto-populated by "javascript:gs.getUserID();" when the form loads; however, the user can change it when requesting an item for someone else.
I would like to set another variable - department - as read only with '111' in it, in case the requested_for source contains the words "MyCompany".
I am unable to do it with the below script:
function onLoad() {
//Type appropriate comment here, and begin script below
var userID = g_form.getValue('requested_for');//gets the user ID
alert('requested for is ' + userID);
var ga = new GlideAjax('getUserSource'); //this is the script include
ga.addParam('sysparm_name', 'getSource'); //this is the function within the script include
ga.addParam('sysparm_usr_source', userID);
ga.getXML(getResponse);
function getResponse(response) {
var values = response.responseXML.documentElement.getAttribute('answer');
if(values == 'yes') {
g_form.setValue('Department_Number', '111');
g_form.setReadOnly('Department_Number', true);
} else if(values == 'no') {
g_form.setReadOnly('Department_Number', false);
}
}
}
The alert to tell me who the requested_for returns "requested for is " (no name, sys_id or anything).
However, it is working when the first line is
var userID = g_user.userID;
instead of var userID = g_form.getValue('requested_for');
But in that case, it does not check the requested_for field...
(Also, gs.log in the script include shows that it is not getting the requested_for value.)
ideas?
harel
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2015 07:56 AM
Guys, thanks for the help and for not giving up!
So HI solved this.
The problem was that I named both the variable set and the variable in it "requested_for ". This (quote from HI) "causes the issue here. Using the same name in both the variable set and variable causing the element search to fail and find the variable set as the element rather than the variable itself."
I would never have thought that the system refers to the actual name in this regard.
I will mark my answer as correct, so other people can benefit from it and from your answers

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2015 07:54 AM
Wait, are you using getReference() or getValue() to get the value of you requested_for variable? You should be using getValue(). It shouldn't matter whether the variable is in a variable set or attached to the item.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2015 08:57 AM
Hi Brad,
I tried both, actually, with the same results. I even tried adding a timer to wait for 3 seconds before invoking the script include, in case the client script needs to take time to wait for the form to fill:
setTimeout(function(){
script here
}, 3000);
So I received the alert after 3 seconds, but still with an "undefined" in it.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2015 11:17 AM
If you're not getting the value then I would guess the issue is with the variable name. I just want to confirm that the name of the variable is requested_for and it is present on the catalog item you're running this on.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2015 11:28 AM
Yes, it is requested_for and it is present there.
I added another field - a text field this time, again with a default value of javascript:gs.getUserID() and queried on it. It is working as expected. That's why I think it might be an issue with the field being a reference field.
[edit]: OK, so a quick update:
I changed the above text field 'test' to a reference field and it is working. When the user has MyCompany in its source, the department number changes.
It is not working on the requested_for field, however.
Normally, I would just go ahead and insert a new field, but I really can't, because it is a variable set which is already implemented in other catalog items...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2015 08:27 AM
Instead of g_form.setValue('Department_Number', '111'); try using it this way
setValue(fieldName, value, displayValue) and provide the displayValue also