- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2017 10:25 AM
I have an OnLoad catalog client script to populate fields based on information from a user's profile. other value is returned properly (email) except for the department field. it's giving me the sys_id for the department instead of displaying the department's name. department field is a reference field in sys_user table.
Your help on this will be appreciated
Catalog �tem
Catalog Client Script
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
-
Team Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2017 11:24 AM
That is what I expected. I have a script include that you can use (and re-use) for populating user values:
Name: usrUtilsAjax
Client callable: true
Script:
var usrUtilsAjax = Class.create();
usrUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUsrDetails: function() {
var result = this.newItem("result");
result.setAttribute("message", "returning user details");
var uID = this.getParameter('sysparm_usr');
var usrObj = new GlideRecord('sys_user');
usrObj.get(uID);
this._addDetail('department', usrObj.department);
this._addDetail('dept_name', usrObj.department.getDisplayValue());
this._addDetail('email', usrObj.email);
},
_addDetail: function(name, value) {
var det = this.newItem("detail");
det.setAttribute("name", name);
det.setAttribute("value", value);
},
type: 'usrUtilsAjax'
});
I included both department and dept_name in case you are populating reference fields or text fields.
You can use something similar to the following for your client script:
function onLoad() {
//Populate client details
var ga = new GlideAjax('usrUtilsAjax');
ga.addParam('sysparm_name', 'getUsrDetails');
ga.addParam('sysparm_usr', newValue);
ga.getXML(setDetails);
}
function setDetails(serverResponse) {
var details = serverResponse.responseXML.getElementsByTagName('detail');
for (i = 0; i < details.length; i++) {
var name = details[i].getAttribute('name');
var value = details[i].getAttribute('value');
if (name == 'dept_name'){g_form.setValue('YOUR_DEPARTMENT_VARIABLE',value);}
if (name == 'email'){g_form.setValue('YOUR_EMAIL_VARIABLE', value);}
}
}
Just be sure to change YOUR_XXX_VARIABLE to be the appropriate variable names you wish to set.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2017 10:31 AM
Hello Fabian,
Two options here.
1.Either change the field from string to reference on catalog item
2. Get the display value by doing GlideAjax call.
Please let me know if you have any questions.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2017 10:32 AM
The department field is a reference field. You may be able to get away with changing user.department to be user.department.name
If this does not work, you may have to use an AJAX call to get the values of the user to set. Let us know if you need help with this and we can provide examples.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2017 11:03 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2017 11:24 AM
That is what I expected. I have a script include that you can use (and re-use) for populating user values:
Name: usrUtilsAjax
Client callable: true
Script:
var usrUtilsAjax = Class.create();
usrUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUsrDetails: function() {
var result = this.newItem("result");
result.setAttribute("message", "returning user details");
var uID = this.getParameter('sysparm_usr');
var usrObj = new GlideRecord('sys_user');
usrObj.get(uID);
this._addDetail('department', usrObj.department);
this._addDetail('dept_name', usrObj.department.getDisplayValue());
this._addDetail('email', usrObj.email);
},
_addDetail: function(name, value) {
var det = this.newItem("detail");
det.setAttribute("name", name);
det.setAttribute("value", value);
},
type: 'usrUtilsAjax'
});
I included both department and dept_name in case you are populating reference fields or text fields.
You can use something similar to the following for your client script:
function onLoad() {
//Populate client details
var ga = new GlideAjax('usrUtilsAjax');
ga.addParam('sysparm_name', 'getUsrDetails');
ga.addParam('sysparm_usr', newValue);
ga.getXML(setDetails);
}
function setDetails(serverResponse) {
var details = serverResponse.responseXML.getElementsByTagName('detail');
for (i = 0; i < details.length; i++) {
var name = details[i].getAttribute('name');
var value = details[i].getAttribute('value');
if (name == 'dept_name'){g_form.setValue('YOUR_DEPARTMENT_VARIABLE',value);}
if (name == 'email'){g_form.setValue('YOUR_EMAIL_VARIABLE', value);}
}
}
Just be sure to change YOUR_XXX_VARIABLE to be the appropriate variable names you wish to set.