- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2022 10:23 AM
Hello,
I am trying to get a GlideAjax query to work based on some reference fields that are on the sys_user table. I have a basic catalog item which has a requested for field and based on that I want to have the company field populate and then expand this out.
The Company field is a reference field on sys_user
Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Get value of the requested_for
var requestor = g_form.getValue('requested_for');
//Debugging
alert(requestor);
// Call the GlideAjax function
var ga = new GlideAjax('commonUtils');
//Name of the function in the GlideAjax script
ga.addParam('sysparm_name', "getUserInfo");
ga.addParam('sysparm_userId', requestor); // Parameter to pass the client side value to Script include
ga.addParam('sysparm_user', newValue); //Onchange field is reference field to sys_user table
ga.getXMLAnswer(function(answer){
var response = JSON.parse(answer);
alert(answer);
g_form.setValue('company', response.company); //used for reference field to department table
g_form.setValue('email', response.email);
g_form.setValue('details', response.company + '\n' + response.email + '\n' + response.street );
});
}
Script Include
var commonUtils = Class.create();
commonUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo : function(){
var userInfo = this.getParameter('sysparm_user');
var userCompany = this.getParameter('sysparm_userId');
var grUser = new GlideRecord('sys_user');
var grComp = new GlideRecord('core_company');
grUser.get(userInfo);
grComp.get(userInfo);
var response = {
"email" : grUser.getValue('email'),
"company" : grUser.getDisplayValue('company'),
};
return JSON.stringify(response);
},
type: 'commonUtils'
});
I can retrieve the values and output them into a single line text or multiLine text field but am unable to populate any reference field
hope someone can help me out
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2022 10:34 AM
Thats because reference fields are expecting to be passed a sys id not display value.
try:
var response = {
"email" : grUser.getValue('email').toString(),
"company" : grUser.getValue('company').toString()
};
Now because you're passing back sys ids, your details will look just include sys ids.
so you could also pass back the display names.
var response = {
email: grUser.getValue('email').toString(),
company: grUser.getValue('company').toString(),
emailDV: grUser.getDisplayValue('email').toString(),
companyDV: grUser.getDisplayValue('company').toString()
};
And then change this line in the client script to:
g_form.setValue('details', response.companyDV + '\n' + response.emailDV + '\n' + response.street );
Hope this helps.
Please mark my answer as Correct/Helpful based on impact
Regards,
Dan H

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2022 10:34 AM
Thats because reference fields are expecting to be passed a sys id not display value.
try:
var response = {
"email" : grUser.getValue('email').toString(),
"company" : grUser.getValue('company').toString()
};
Now because you're passing back sys ids, your details will look just include sys ids.
so you could also pass back the display names.
var response = {
email: grUser.getValue('email').toString(),
company: grUser.getValue('company').toString(),
emailDV: grUser.getDisplayValue('email').toString(),
companyDV: grUser.getDisplayValue('company').toString()
};
And then change this line in the client script to:
g_form.setValue('details', response.companyDV + '\n' + response.emailDV + '\n' + response.street );
Hope this helps.
Please mark my answer as Correct/Helpful based on impact
Regards,
Dan H
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2022 10:35 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2022 10:41 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2022 10:51 AM
What is the alert saying for company now when you load the form?
Also its probably not working because you're sending a company sys id existing in table [core_company] to the company field that you said is referencing [sys_user]. Can you change the reference to the company table. That way a record will be found.
Hope this helps.
Please mark my answer as Correct/Helpful based on impact
Regards,
Dan H