- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 09:11 AM
I'm looking to populate several fields on a catalog item by looking up values on a user record based on their workID#. I've done this successfully for one variable using a catalog client script and a script include. However I don't want to have to create multiple catalog client scripts and script includes and would rather combine it all into 1 of each. I'm having trouble with how to script each for multiple variables. Any help would be great, thanks!
here's the catalog client script, in here you see the 4 variables I want to populate off the user record. IS THIS SCRIPT CORRECT?
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == ''){
g_form.setValue('first_name','');//name of field on form you want to auto populate
g_form.setValue('last_name','');//name of field on form you want to auto populate
g_form.setValue('supervisor','');//name of field on form you want to auto populate
g_form.setValue('emp_location','');//name of field on form you want to auto populate
}
var ga = new GlideAjax('u_employee_details_lookup_Ajax');//name of script include
ga.addParam('sysparm_name', 'getEmployeeDetails');//name of function on script include
ga.addParam('sysparm_user', g_form.getValue('emp_workday_id'));//name of field on form triggering call
ga.getXML(EmployeeDetailsLookup); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)
}
// Callback function to process the response returned from the server
function EmployeeDetailsLookup(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert('answer: ' + answer);
var answers = answer.split('|');
g_form.setValue('first_name',answers[0]);
g_form.setValue('last_name',answers[1]);
g_form.setValue('supervisor',answers[2]);
g_form.setValue('emp_location',answers[3]);
}
and here is the script include: NOT SURE WHAT TO DO WITH THIS TO ACCOMMODATE FOR THE 4 VARIABLES, I KNOW THIS IS INCOMPLETE.
var u_employee_details_lookup_Ajax = Class.create();
u_employee_details_lookup_Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEmployeeDetailsfunction(){
var retVal; // Return value
var user = this.getParameter('sysparm_user');
var firstNameRec = new GlideRecord('sys_user');//table where desired variable lives
firstNameRec.addQuery('user_name',user);
firstNameRec.query();
// Query user records
if(firstNameRec.next())
{
retVal = firstNameRec.first_name;//name of field with info you want to grab
}
return retVal;
},
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2017 08:48 AM
Hi Pat,
You can return a string from script include with all value separated by comma.
for example:
retVal = firstNameRec.first_name+','+firstNameRec.last_name+','+firstNameRec.supervisor+','+firstNameRec.emp_location;
And split this string and set the each field values in Client script:(Like below)
var answers = answer.split(',');
g_form.setValue('first_name',answers[0]);
g_form.setValue('last_name',answers[1]);
g_form.setValue('supervisor',answers[2]);
g_form.setValue('emp_location',answers[3]);
Thanks,
Arindam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 12:13 PM
Hi Patrick,
The response back from script include is an array of objects. So I believe you should do an array[i][X] instead of just array[X]. Something along the lines of below:
answer = answer.evalJSON(); //Transform the JSON string to an object
for( var i=0 ; i < answer.length ; i++){//loop into the array
g_form.setValue('first_name',answer[i][0]);
g_form.setValue('last_name',answer[i][1]);
g_form.setValue('supervisor',answer[i][2]);
g_form.setValue('emp_location',answer[i][3]);
}
Please put in debug statements (gs.log or gs.addinfomessage) if this does not work to see what is coming back from the script include in the answer object to isolate the issue.
Manish

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 12:20 PM
I do not know why, but I was having difficulties making that solution work within the new Service Portal, so I used a similar method, but without using JSON. I modified what you had to use this method. Hopefully I have the correct fields you are pulling from.
Add the following Functions to your current script include:
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('first_name', usrObj.first_name);
this._addDetail('last_name', usrObj.last_name);
this._addDetail('location', usrObj.location);
this._addDetail('location_display', usrObj.location.getDisplayValue());
this._addDetail('manager', usrObj.manager);
this._addDetail('manager_display', usrObj.manager.getDisplayValue());
},
_addDetail: function(name, value) {
var det = this.newItem("detail");
det.setAttribute("name", name);
det.setAttribute("value", value);
},
Here are the changes to the Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == ''){
g_form.setValue('first_name','');//name of field on form you want to auto populate
g_form.setValue('last_name','');//name of field on form you want to auto populate
g_form.setValue('supervisor','');//name of field on form you want to auto populate
g_form.setValue('emp_location','');//name of field on form you want to auto populate
}
if (newValue != oldValue) {
var ga = new GlideAjax('u_employee_details_lookup_Ajax');//name of script include
ga.addParam('sysparm_name', 'getSrvDetails'); //run the getSrvDetails function
ga.addParam('sysparm_usr', newValue); //pass the newValue as an inupt parameter
ga.getXML(setDetails); //get the answer from the script include, then run the setDetails function
}
}
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 == 'first_name') {
g_form.setValue('first_name', value);
}
if (name == 'last_name') {
g_form.setValue('last_name', value);
}
if (name == 'manager') {
g_form.setValue('supervisor', value);
}
if (name == 'location') {
g_form.setValue('emp_location', value);
}
}
}
My guess is that the manager and location variables are reference type variables. If not, then use manager_display and location_display accordingly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 02:29 PM
Hi Christopher,
thanks for the reply. I've added your scripts and it's not working yet. Here's what I have: my edits/notes are in BOLD.
client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == ''){
g_form.setValue('first_name','');//name of field on form you want to auto populate
g_form.setValue('last_name','');//name of field on form you want to auto populate
g_form.setValue('supervisor','');//name of field on form you want to auto populate
g_form.setValue('emp_location','');//name of field on form you want to auto populate
}
if (newValue != oldValue) {
var ga = new GlideAjax('u_employee_details_lookup_Ajax');//name of script include
ga.addParam('sysparm_name', 'getSrvDetails'); //run the getSrvDetails function
ga.addParam('sysparm_user', newValue); // YOU HAD PUT 'sysparm_usr', was that a typo?
ga.getXML(setDetails); //get the answer from the script include, then run the setDetails function
}
}
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 == 'first_name') {
g_form.setValue('first_name', value);
}
if (name == 'last_name') {
g_form.setValue('last_name', value);
}
if (name == 'manager') {
g_form.setValue('supervisor', value);
}
if (name == 'location') {
g_form.setValue('emp_location', value);
}
}
}
and my script include:
var u_employee_details_lookup_Ajax = Class.create();
u_employee_details_lookup_Ajax.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('first_name', usrObj.first_name);
this._addDetail('last_name', usrObj.last_name);
this._addDetail('location', usrObj.location);
this._addDetail('location', usrObj.location.getDisplayValue());//YOU HAD 'location_display', what field does this want to be?
this._addDetail('manager', usrObj.manager);
this._addDetail('manager', usrObj.manager.getDisplayValue());//YOU HAD 'manager_display', what field does this want to be?
},
_addDetail: function(name, value) {
var det = this.newItem("detail");
det.setAttribute("name", name);
det.setAttribute("value", value);
},
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 04:41 PM
Please try below script, i was able to get the alerts for all the required values, hope this helps.
Script Include:
var u_employee_details_lookup_Ajax = Class.create();
u_employee_details_lookup_Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEmployeeDetails: function(){
var object = {};
var EmployeeDetailsRec = new GlideRecord('sys_user');//table where desired variable lives
EmployeeDetailsRec.get(this.getParameter('sysparm_user'));
object.firstName = EmployeeDetailsRec.getDisplayValue('first_name');
object.lastName = EmployeeDetailsRec.getDisplayValue('last_name');
object.supervisory = EmployeeDetailsRec.getDisplayValue('manager');
object.location = EmployeeDetailsRec.getDisplayValue('location');
var json = new JSON();
return json.encode(object);//JSON formatted string
},
type: 'u_employee_details_lookup_Ajax'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == ''){
g_form.setValue('first_name','');
g_form.setValue('last_name','');
g_form.setValue('supervisor','');
g_form.setValue('emp_location','');
}
if (newValue != oldValue) {
var ga = new GlideAjax('u_employee_details_lookup_Ajax');
ga.addParam('sysparm_name', 'getEmployeeDetails');
ga.addParam('sysparm_user', newValue);
ga.getXML(EmployeeDetailsLookup);
}
}
// Callback function to process the response returned from the server
function EmployeeDetailsLookup(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.evalJSON(); //Transform the JSON string to an object
alert(answer.firstName);
alert(answer.lastName);
alert(answer.supervisory);
alert(answer.location);
for( var i=0 ; i < answer.length ; i++){//loop into the array
g_form.setValue('first_name', answer.firstName);
g_form.setValue('last_name', answer.lastName);
g_form.setValue('supervisor',answer.supervisory);
g_form.setValue('emp_location',answer.location);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 04:59 PM
thanks Shishir,
unfortunately that's not working just yet...it's returning 4 empty alerts.