- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-09-2020 04:21 PM
Hi, I got no issue displaying all the variables below except just cannot get 'Cost_Centre' to display its value.
The below code will only display sys_id for g_form.setValue('Cost_Centre',requestor.cost_center) on the form;
Then if I tried 'requestor.cost_center.getDisplayValue()' already, it just not showing anything on the form value at all.
What's wrong with this value?
---------------------------------------------------------------------------------
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(g_form.getValue('Employee_Name')!='')
{
//var requestor = g_user.userID;
var requestor = g_form.getReference('Employee_Name', autopopulate);
}
}
function autopopulate(requestor)
{
if (requestor)
{
g_form.setValue('Line_Manager',requestor.manager);
g_form.setValue('Job_Title',requestor.title);
g_form.setValue('contact_number',requestor.phone);
g_form.setValue('Department',requestor.department);
g_form.setValue('site',requestor.location);
g_form.setValue('Entity',requestor.u_entity);
g_form.setValue('emp_number',requestor.employee_number);
g_form.setValue('emp_status',requestor.active);
g_form.setValue('Cost_Centre',requestor.cost_center);
}
}
Regards,
Simon.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-10-2020 04:13 PM
Hi Simon,
I believe you want to get details of an employee from user table, if my understanding is correct then I would recommend you to use json parameter and get all the values from 'sys_user' table with the help of Glide Ajax and creating a script include. Since, getReference() is the least efficient function.
You could refer to the below link for more details on getReference
getReference() - best practice or not?
If you would like to use my approach you can use the scripts below:
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('Employee_Details');
ga.addParam('sysparm_name', 'details');
ga.addParam('sysparm_employee', newValue);
ga.getXML(setDetails);
}
function setDetails(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != 'false') {
var requestor = answer.evalJSON();
g_form.setValue('Line_Manager', requestor.manager);
g_form.setValue('Job_Title', requestor.title);
g_form.setValue('contact_number', requestor.phone);
g_form.setValue('Department', requestor.department);
g_form.setValue('site', requestor.location);
g_form.setValue('Entity', requestor.u_entity);
g_form.setValue('emp_number', requestor.employee_number);
g_form.setValue('emp_status', requestor.active);
g_form.setValue('Cost_Centre', requestor.cost_center);
}
}
Script Include:
Name: Employee_Details
Client Callable: True(Check the checkbox)
var Employee_Details = Class.create();
Employee_Details.prototype = Object.extendsObject(AbstractAjaxProcessor, {
details: function() {
var value = 'false';
var gr = new GlideRecord('sys_user');
if (gr.get(this.getParameter('sysparm_employee'))) {
var object = {};
object.manager = gr.manager;
object.title = gr.title;
object.phone = gr.phone;
object.department = gr.department;
object.location = gr.location;
object.u_entity = gr.entity;
object.employee_number = gr.employee_number;
object.active = gr.active;
object.cost_center = gr.getDisplayValue('cost_center');
value = JSON.stringify(object);
}
return value;
},
type: 'Employee_Details'
});
Kindly mark my answer as Correct and Helpful based on the Impact.
Regards,
Alok

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-09-2020 04:24 PM
.getDisplayValue() will not work on client script. You will have to use GlideAjax to do so. Use my article to create your SI and get all the fields you want.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-09-2020 06:19 PM
Haven't tested this but something like below.
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(g_form.getValue('Employee_Name')!='')
{
//var requestor = g_user.userID;
var requestor = g_form.getReference('Employee_Name', _autopopulate);
}
}
function _autopopulate(requestor)
{
if (requestor)
{
g_form.setValue('Line_Manager',requestor.manager);
g_form.setValue('Job_Title',requestor.title);
g_form.setValue('contact_number',requestor.phone);
g_form.setValue('Department',requestor.department);
g_form.setValue('site',requestor.location);
g_form.setValue('Entity',requestor.u_entity);
g_form.setValue('emp_number',requestor.employee_number);
g_form.setValue('emp_status',requestor.active);
g_form.setValue('Cost_Centre', _costcentrename(requestor.cost_center));
}
}
function _costcentrename(costCenterId)
{
var ajax = new GlideAjax('getCostCenterName');
ajax.addParam('sysparm_name', 'getDisplayName');
ajax.addParam('sysparm_id', costCenterId);
ajax.getXMLAnswer(function(answer) {
return answer;
});
}
Script Include:
var getCostCenterName = Class.create();
getCostCenterName .prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDisplayName: function() {
var costCentreId = this.getParameter('sysparm_id');
var grCostCentre = new GlideRecord('');
if (grCostCentre.get(costCentreId)) {
return grCostCentre.getDisplayValue;
}
return '';
},
type: 'getCostCenterName'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-09-2020 08:07 PM
Hi hozawa,
I have added the code and created a new script include, and I got these warnings & errors in console (also attached screenshot for better viewing)
Array(9) outputToConsole @ js_includes_sp.jsx?v=08-28-2020_1009&lp=Fri_Sep_04_00_43_46_PDT_2020&c=20_298:43877
Array(6) outputToConsole @ js_includes_sp.jsx?v=08-28-2020_1009&lp=Fri_Sep_04_00_43_46_PDT_2020&c=20_298:43877
getMessage (key="Attachment(s) are not added"): synchronous use not supported in Mobile or Service Portal unless message is already cached
getMessage (key="Please wait, attachment deletion in progress"): synchronous use not supported in Mobile or Service Portal unless message is already cached
getMessage (key="Remove {0} from {1}"): synchronous use not supported in Mobile or Service Portal unless message is already cached
Failed to load resource: the server responded with a status of 404 (Not Found)
Unhandled exception in GlideAjax.
Thx!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-09-2020 11:12 PM
Sorry, forgot to add comment. Need to specify control centre table name to query.
var grCostCentre = new GlideRecord('<control centre table name>');