- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2020 09:39 AM
Hi,
I am trying to write a client script on change of "contact" record, I want to auto populate the fields like manager's name, email address etc. Manager field is a reference to sys_user table.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var contact = g_form.getReference('contact', callBack);
}
function callBack(contact)
{
g_form.setValue('u_employee_last_name', contact.last_name);
g_form.setValue('u_employee_first_name', contact.first_name);
g_form.setValue('u_work_email_address', contact.email);
g_form.setValue('u_manager_name', contact.manager);
g_form.setValue('u_manager_first_name', contact.manager.first_name);
g_form.setValue('u_manager_last_name', contact.manager.last_name);
}
Here is what I have so far and the results are turned out to be "undefined"
Please help. Thank you
Solved! Go to Solution.
- Labels:
-
Customer Service Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2020 12:33 PM
Thanks for the detailed update.
Can you make your Manager (custom field) to be of type Reference instead of String.
If so, in the above script you need to comment last 2 lines where you set first name & last name.
Then get another onChage() client script written that takes the manager value & gets the first name & last name with the code similar to one you used above to get in Contact's first name & last name.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2020 12:33 PM
Thanks for the detailed update.
Can you make your Manager (custom field) to be of type Reference instead of String.
If so, in the above script you need to comment last 2 lines where you set first name & last name.
Then get another onChage() client script written that takes the manager value & gets the first name & last name with the code similar to one you used above to get in Contact's first name & last name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2020 12:58 PM
Thanks Jaspal! That appeared to work.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2020 10:07 AM
Hello,
Try below script and Change fields as per your requirement,
Script Include
var autopopulatefield = Class.create();
autopopulatefield.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getInfo: function(){
var obj={};
obj.location='';
obj.company='';
obj.manager='';
obj.department='';
var id=this.getParameter('sysparm_user_id');
var gr= new GlideRecord('sys_user');
gr.addQuery('sys_id',id);
gr.query();
// if(gr.get(id)){
if(gr.next()){
obj.location=gr.location.name.toString();
obj.company=gr.company.name.toString();
obj.manager=gr.manager.name.toString();
obj.department=gr.department.name.toString();
//obj.location=gr.getDispalyValue('field_name');
// obj.company=gr.getDispalyValue('company');
// obj.email=gr.getDispalyValue('email');
// obj.department=gr.getDispalyValue('department');
}
var json = new JSON();
var data = json.encode(obj);
return data;
},
//type: 'autopopulatefield'
});
Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//var id = g_form.getValue('u_name');
var ga = new GlideAjax('autopopulatefield');
ga.addParam('sysparm_name','getInfo');
ga.addParam('sysparm_user_id',newValue);
ga.getXML(CallBack);
function CallBack(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
var user=JSON.parse(answer);
var com=user.company;
g_form.setValue('Officelocation', user.location);
g_form.setReadOnly('Officelocation', true);
g_form.setValue("company",com);
g_form.setReadOnly('company', true);
g_form.setValue('manager', user.manager);
g_form.setReadOnly('manager', true);
g_form.setValue('department', user.department);
g_form.setReadOnly('department', true);
}
}
Please mark my answer correct and helpful as per your impact.
Thanks
Tanushree
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2020 11:00 AM
Hi Tanushree,
I tried your approach but the data is not getting populated for any of the fields. Not sure what I am doing wrong. Please help
Here is my script includes
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('sn_customerservice.AutoPopulateSuntaxForm');
ga.addParam('sysparm_name', 'getInfo');
ga.addParam('sysparm_user_id', newValue);
ga.getXML(CallBack);
function CallBack(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var user = JSON.parse(answer);
g_form.setValue('u_employee_last_name', user.employee_last_name);
g_form.setReadOnly('u_employee_last_name', true);
g_form.setValue('u_employee_first_name', user.employee_first_name);
g_form.setReadOnly('u_employee_first_name', true);
g_form.setValue('u_manager_name', user.manager);
g_form.setReadOnly('u_manager_name', true);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2020 09:25 PM
Make sure you have to click on client callable checkbox in script include, that's why you can call it on client script.
Thanks
Tanushree