- 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 05:08 PM
I tweaked a couple things to fix field names, here's what I've got so far...again just returning 4 empty alerts.
CATALOG 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.supervisor);//FIXED THIS VARIABLE
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.supervisor);//FIXED THIS VARIABLE
g_form.setValue('emp_location',answer.location);
}
}
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.supervisor = EmployeeDetailsRec.getDisplayValue('manager');//FIXED THIS object.supervisor
object.location = EmployeeDetailsRec.getDisplayValue('location');
var json = new JSON();
return json.encode(object);//JSON formatted string
},
type: 'u_employee_details_lookup_Ajax'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 05:17 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 06:27 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 08:08 PM
Not sure what's wrong with JSON then, please check if below script works. works for me.
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 result = response.responseXML.getElementsByTagName("result");
alert(result[0].getAttribute("first_name"));
alert(result[0].getAttribute("last_name"));
alert(result[0].getAttribute("supervisor"));
alert(result[0].getAttribute("location"));
g_form.setValue('first_name', result[0].getAttribute("first_name"));
g_form.setValue('last_name', result[0].getAttribute("last_name"));
g_form.setValue('supervisor',result[0].getAttribute("supervisor"));
g_form.setValue('emp_location',result[0].getAttribute("location"));
}
}
Script Include:
var u_employee_details_lookup_Ajax = Class.create();
u_employee_details_lookup_Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEmployeeDetails: function(){
var EmployeeDetailsRec = new GlideRecord('sys_user');
EmployeeDetailsRec.get(this.getParameter('sysparm_user'));
var result = this.newItem("result");
result.setAttribute('first_name', EmployeeDetailsRec.first_name);
result.setAttribute('last_name', EmployeeDetailsRec.last_name);
result.setAttribute('location', EmployeeDetailsRec.getDisplayValue('location'));
result.setAttribute('supervisor', EmployeeDetailsRec.getDisplayValue('manager'));
},
type: 'u_employee_details_lookup_Ajax'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2017 08:26 AM
Hi Shishir, that unfortunately still not working. To clarify, these are the variable names:
these are the field names on the form I want to populate
first_name (string)
last_name (string)
supervisor (reference field)
emp_location (reference field)
these are the field names on the user record that I want pull the values from:
first_name (string)
last_name (string)
manager (reference field)
location (reference field)
the field name on the form I want to have trigger the clients script/script include:
emp_workday_id
the field on the user record that is tying to the "emp_workday_id" value:
u_workday_employee_number
does this info change your script at all?