- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-31-2019 06:33 AM
Hi,
Please find below screenshots of my catalog item. Requested for is a reference field to table sys_user. When I select a user's name in requested for, the user's values on the fields on right side should be automatically populated from sys_user table.
For eg - abel tuter is selected so first name, last name etc should be populated from sys_user table. The values which are not present should remain empty.
Please help me on how to do this with the code as I am new to ServiceNow and not very good at scripting..
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-31-2019 06:44 AM
Hi
You need to create a onChange client script and client callable script include for this purpose. See below :-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var id = g_form.getValue('u_reference_1');//newValue
var ga = new GlideAjax('GetUserDetailsAjax');
ga.addParam('sysparm_name','getInfo');
ga.addParam('sysparm_user_id',id);
ga.getXML(CallBack);
function CallBack(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
var user=JSON.parse(answer);
//g_form.setValue('req_email', user.email);
//g_form.setValue('req_phone', user.phone);
//g_form.setValue('address1', user.street);
alert(user.location);
//g_form.setValue('city_state_zip', user.location);
}
}
Client Callable Script Include :-
var GetUserDetailsAjax = Class.create();
GetUserDetailsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getInfo: function(){
var obj={};
obj.location='';
obj.email='';
obj.phone='';
obj.street='';
var id=this.getParameter('sysparm_user_id');
var gr= new GlideRecord('sys_user');
if(gr.get(id)){
if(gr.location!='')
obj.location=gr.location.city.toString();
obj.zip=gr.zip.toString();
obj.street=gr.street.toString();
obj.phone=gr.phone.toString();
obj.email=gr.email.toString();
//here you will get all the required fields from sys_user that you want to show on your form.
}
return JSON.stringify(obj);
},
type: 'GetUserDetailsAjax'
});
Let me know.
Regards,
Omkar Mone.
www.dxsherpa.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2019 10:36 AM
Hi Omkar, any idea why first name is not coming up for these users?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2019 08:53 PM
Hi
Can you post a screenshot of the sys_user form of any of one user whose first name is not coming.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2019 10:32 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2019 11:11 PM
Try below code,
var GetUserDetailsAjax = Class.create();
GetUserDetailsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getInfo: function(){
var obj={};
obj.first_name='';
obj.last_name='';
obj.email='';
obj.vendor_name='';
obj.location='';
obj.department='';
obj.beeline_assignment_number='';
obj.manager='';
obj.contracter_end_date='';
obj.phone_number='';
// obj.street='';
var id=this.getParameter('sysparm_user_id');
var gr= new GlideRecord('sys_user');
if(gr.get(id)){
if(gr.location!=''){
obj.first_name = gr.first_name.toString();
obj.last_name = gr.last_name.toString();
obj.email=gr.email.toString();
obj.vendor_name =gr.u_vendor_name.toString();
obj.location=gr.location.name.toString();
obj.department=gr.department.name.toString();
obj.beeline_assignment_number = gr.department.id.toString();
obj.manager=gr.manager.getDisplayValue().toString();
obj.contracter_end_date=gr.u_end_date.toString();
// obj.zip=gr.zip.toString();
//obj.street=gr.street.toString();
obj.phone_number=gr.mobile_phone.toString();
}
}
var json = new JSON();
var user_data = json.encode(obj);
return user_data;
},
type: 'GetUserDetailsAjax'
});
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var id = g_form.getValue('requested_for');//newValue
var ga = new GlideAjax('GetUserDetailsAjax');
ga.addParam('sysparm_name','getInfo');
ga.addParam('sysparm_user_id',id);
ga.getXML(CallBack);
function CallBack(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
var user= answer.evalJSON();
g_form.addInfoMessage("first name "+user.first_name);
g_form.setValue('first_name',user.first_name);
g_form.setValue('last_name', user.last_name);
g_form.setValue('email_address', user.email);
g_form.setValue('vendor_name', user.u_vendor_name);
g_form.setValue('location_number', user.location);
g_form.setValue('department', user.department);
g_form.setValue('current_assignment_number', user.beeline_assignment_number);
g_form.setValue('manager', user.manager);
g_form.setValue('end_date', user.contracter_end_date);
g_form.setValue('phone_number', user.phone_number);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2019 11:29 PM